Upload multiple files and publish vars with fsockopen

I want both send multiple files and send vars to http request using fsockopen ..

We came up with this code, but I don’t know how to send post vars with files !? Compiled the line in which the data request is built after publication, but do not know how to put it in the request!?: (

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); 

Output

 -------------------- 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 -------------------- 
+4
source share
2 answers

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); 

Output

 -------------------- 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 -------------------- 
+3
source

Try the following:

.$file_contents.$crlf.$crlf; β†’ .$file_contents.$crlf;

-1
source

Source: https://habr.com/ru/post/1481672/


All Articles