userland multipart/form-data handler

前端 未结 3 1948
谎友^
谎友^ 2020-12-03 06:28

I\'m looking for a drop-in include script / class that dissects multipart/form-data and fills up $_POST(+raw) and $_FILES from it. Usu

3条回答
  •  攒了一身酷
    2020-12-03 06:36

    It's late and I can't test this at the moment but the following should do what you want:

    //$boundary = null;
    
    if (is_resource($input = fopen('php://input', 'rb')) === true)
    {
    
        while ((feof($input) !== true) && (($line = fgets($input)) !== false))
        {
            if (isset($boundary) === true)
            {
                $content = null;
    
                while ((feof($input) !== true) && (($line = fgets($input)) !== false))
                {
                    $line = trim($line);
    
                    if (strlen($line) > 0)
                    {
                        $content .= $line . ' ';
                    }
    
                    else if (empty($line) === true)
                    {
                        if (stripos($content, 'name=') !== false)
                        {
                            $name = trim(stripcslashes(preg_replace('~.*name="?(.+)"?.*~i', '$1', $content)));
    
                            if (stripos($content, 'Content-Type:') !== false)
                            {
                                $tmpname = tempnam(sys_get_temp_dir(), '');
    
                                if (is_resource($temp = fopen($tmpname, 'wb')) === true)
                                {
                                    while ((feof($input) !== true) && (($line = fgets($input)) !== false) && (strpos($line, $boundary) !== 0))
                                    {
                                        fwrite($temp, preg_replace('~(?:\r\n|\n)$~', '', $line));
                                    }
    
                                    fclose($temp);
                                }
    
                                $FILES[$name] = array
                                (
                                    'name' => trim(stripcslashes(preg_replace('~.*filename="?(.+)"?.*~i', '$1', $content))),
                                    'type' => trim(preg_replace('~.*Content-Type: ([^\s]*).*~i', '$1', $content)),
                                    'size' => sprintf('%u', filesize($tmpname)),
                                    'tmp_name' => $tmpname,
                                    'error' => UPLOAD_ERR_OK,
                                );
                            }
    
                            else
                            {
                                $result = null;
    
                                while ((feof($input) !== true) && (($line = fgets($input)) !== false) && (strpos($line, $boundary) !== 0))
                                {
                                    $result .= preg_replace('~(?:\r\n|\n)$~', '', $line);
                                }
    
                                if (array_key_exists($name, $POST) === true)
                                {
                                    if (is_array($POST[$name]) === true)
                                    {
                                        $POST[$name][] = $result;
                                    }
    
                                    else
                                    {
                                        $POST[$name] = array($POST[$name], $result);
                                    }
                                }
    
                                else
                                {
                                    $POST[$name] = $result;
                                }
                            }
                        }
    
                        if (strpos($line, $boundary) === 0)
                        {
                            //break;
                        }
                    }
                }
            }
    
            else if ((is_null($boundary) === true) && (strpos($line, 'boundary=') !== false))
            {
                $boundary = "--" . trim(preg_replace('~.*boundary="?(.+)"?.*~i', '$1', $line));
            }
        }
    
        fclose($input);
    }
    
    echo '
    ';
    print_r($POST);
    echo '
    '; echo '
    '; echo '
    ';
    print_r($FILES);
    echo '
    ';

提交回复
热议问题