php – 使用fsockopen上传多个文件并发布变量

我想要使​​用fsockopen在http请求中发送多个文件并发布变量.

已经拿出这个代码,但我不知道如何发送文件的post vars!?已经取消注释了构建post数据查询的行,但是不知道如何将它放在请求中!? 🙁

的index.php

$post_vars = [
    'label' => 'description of the upload'
];

$files = [
    'upload_file_1.txt',
    'upload_file_2.txt'
];

try{
    $boundary = sha1(1);
    $crlf = "\r\n";
    $body = '';

    foreach($files as $file){
        $finfo = new \finfo(FILEINFO_MIME);
        $mimetype = $finfo->file($file);

        $file_contents = quoted_printable_encode(file_get_contents($file));

        $body .= '--'.$boundary.$crlf
            .'Content-Disposition: form-data; name="files[]"; filename="'.basename($file).'"'.$crlf
            .'Content-Type: '.$mimetype.$crlf
            .'Content-Length: '.strlen($file_contents).$crlf
            .'Content-Type: application/octet-stream'.$crlf.$crlf
            .$file_contents.$crlf;
    }

    $body .= '--'.$boundary.'--';

    //$post_data = http_build_query($post_vars);

    $response = '';
    if($fp = fsockopen('localhost', 80, $errno, $errstr, 20)){
        $write = "POST /api_test/filepost/post.php HTTP/1.1\r\n"
            ."Host: localhost\r\n"
            ."Content-type: multipart/form-data; boundary=".$boundary."\r\n"
            ."Content-Length: ".strlen($body)."\r\n"
            ."Connection: Close\r\n\r\n"
            .$body;
        fwrite($fp, $write);

        echo "-------------------- REQUEST START --------------------\n".$write."\n-------------------- REQUEST END --------------------\n\n\n";

        while($line = fgets($fp)){
            if($line !== false){
                $response .= $line;
            }
        }

        fclose($fp);

        echo "-------------------- RESPONSE START --------------------\n".$response."\n-------------------- RESPONSE END --------------------\n\n";
    }
    else{
        throw new Exception("$errstr ($errno)");
    }
}
catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}

post.php中

echo "get\n";
print_r($_GET);

echo "post\n";
print_r($_POST);

echo "files\n";
print_r($_FILES);

产量

-------------------- REQUEST START --------------------
POST /api_test/filepost/post.php HTTP/1.1
Host: localhost
Content-type: multipart/form-data; boundary=356a192b7913b04c54574d18c28d46e6395428ab
Content-Length: 540
Connection: Close

--356a192b7913b04c54574d18c28d46e6395428ab
Content-Disposition: form-data; name="files[]"; filename="upload_file_1.txt"
Content-Type: text/plain; charset=us-ascii
Content-Length: 18
Content-Type: application/octet-stream

contents of file 1
--356a192b7913b04c54574d18c28d46e6395428ab
Content-Disposition: form-data; name="files[]"; filename="upload_file_2.txt"
Content-Type: text/plain; charset=us-ascii
Content-Length: 18
Content-Type: application/octet-stream

contents of file 2
--356a192b7913b04c54574d18c28d46e6395428ab--
-------------------- REQUEST END --------------------


-------------------- RESPONSE START --------------------
HTTP/1.1 200 OK
Date: Mon, 20 May 2013 08:46:21 GMT
Server: Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By: PHP/5.4.3
Content-Length: 803
Connection: close
Content-Type: text/plain

get
Array
(
)
post
Array
(
)
files
Array
(
    [files] => Array
        (
            [name] => Array
                (
                    [0] => upload_file_1.txt
                    [1] => upload_file_2.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\php1827.tmp
                    [1] => C:\wamp\tmp\php1828.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 18
                    [1] => 18
                )

        )

)

-------------------- RESPONSE END --------------------

最佳答案 的index.php

$post_vars = [
    'label' => 'description of the upload'
];

$files = [
    'upload_file_1.txt',
    'upload_file_2.txt'
];

try{
    $boundary = sha1(1);
    $crlf = "\r\n";
    $body = '';

    foreach($post_vars as $key => $value){
        $body .= '--'.$boundary.$crlf
            .'Content-Disposition: form-data; name="'.$key.'"'.$crlf
            .'Content-Length: '.strlen($value).$crlf.$crlf
            .$value.$crlf;
    }

    foreach($files as $file){
        $finfo = new \finfo(FILEINFO_MIME);
        $mimetype = $finfo->file($file);

        $file_contents = quoted_printable_encode(file_get_contents($file));

        $body .= '--'.$boundary.$crlf
            .'Content-Disposition: form-data; name="files[]"; filename="'.basename($file).'"'.$crlf
            .'Content-Type: '.$mimetype.$crlf
            .'Content-Length: '.strlen($file_contents).$crlf
            .'Content-Type: application/octet-stream'.$crlf.$crlf
            .$file_contents.$crlf;
    }

    $body .= '--'.$boundary.'--';

    //$post_data = http_build_query($post_vars);

    $response = '';
    if($fp = fsockopen('localhost', 80, $errno, $errstr, 20)){
        $write = "POST /api_test/filepost/post.php HTTP/1.1\r\n"
            ."Host: localhost\r\n"
            ."Content-type: multipart/form-data; boundary=".$boundary."\r\n"
            ."Content-Length: ".strlen($body)."\r\n"
            ."Connection: Close\r\n\r\n"
            .$body;
        fwrite($fp, $write);

        echo "-------------------- REQUEST START --------------------\n".$write."\n-------------------- REQUEST END --------------------\n\n\n";

        while($line = fgets($fp)){
            if($line !== false){
                $response .= $line;
            }
        }

        fclose($fp);

        echo "-------------------- RESPONSE START --------------------\n".$response."\n-------------------- RESPONSE END --------------------\n\n";
    }
    else{
        throw new Exception("$errstr ($errno)");
    }
}
catch(Exception $e){
    echo 'Error: '.$e->getMessage();
}

post.php中

echo "get\n";
print_r($_GET);

echo "post\n";
print_r($_POST);

echo "files\n";
print_r($_FILES);

产量

-------------------- REQUEST START --------------------
POST /api_test/filepost/post.php HTTP/1.1
Host: localhost
Content-type: multipart/form-data; boundary=356a192b7913b04c54574d18c28d46e6395428ab
Content-Length: 679
Connection: Close

--356a192b7913b04c54574d18c28d46e6395428ab
Content-Disposition: form-data; name="label"
Content-Length: 25

description of the upload
--356a192b7913b04c54574d18c28d46e6395428ab
Content-Disposition: form-data; name="files[]"; filename="upload_file_1.txt"
Content-Type: text/plain; charset=us-ascii
Content-Length: 18
Content-Type: application/octet-stream

contents of file 1
--356a192b7913b04c54574d18c28d46e6395428ab
Content-Disposition: form-data; name="files[]"; filename="upload_file_2.txt"
Content-Type: text/plain; charset=us-ascii
Content-Length: 18
Content-Type: application/octet-stream

contents of file 2
--356a192b7913b04c54574d18c28d46e6395428ab--
-------------------- REQUEST END --------------------


-------------------- RESPONSE START --------------------
HTTP/1.1 200 OK
Date: Mon, 20 May 2013 09:07:58 GMT
Server: Apache/2.2.22 (Win32) PHP/5.4.3
X-Powered-By: PHP/5.4.3
Content-Length: 844
Connection: close
Content-Type: text/plain

get
Array
(
)
post
Array
(
    [label] => description of the upload
)
files
Array
(
    [files] => Array
        (
            [name] => Array
                (
                    [0] => upload_file_1.txt
                    [1] => upload_file_2.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\phpE35D.tmp
                    [1] => C:\wamp\tmp\phpE35E.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 18
                    [1] => 18
                )

        )

)

-------------------- RESPONSE END --------------------
点赞