Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用fsockopen上载多个文件并发布变量_Php_Fsockopen - Fatal编程技术网

Php 使用fsockopen上载多个文件并发布变量

Php 使用fsockopen上载多个文件并发布变量,php,fsockopen,Php,Fsockopen,我想用fsockopen在http请求中发送多个文件和post变量 我已经想出了这个代码,但是我不知道如何发送带有文件的post VAR!?已取消对生成post数据查询的行的注释,但不知道如何将其放入请求中!?:( index.php post.php 输出 试试这个: $file\u contents.$crlf.$crlf;->$file\u contents.$crlf;index.php post.php 输出 为什么每个块有两次内容类型字段? $post_vars = [ 'l

我想用fsockopen在http请求中发送多个文件和post变量

我已经想出了这个代码,但是我不知道如何发送带有文件的post VAR!?已取消对生成post数据查询的行的注释,但不知道如何将其放入请求中!?:(

index.php post.php 输出 试试这个:

$file\u contents.$crlf.$crlf;
->
$file\u contents.$crlf;

index.php post.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();
}
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 --------------------
$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();
}
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 --------------------