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 --------------------
source share