php - Upload multiple files and post vars with fsockopen -


i want both send multiple files , post vars in http request fsockopen..

have come code, don't know how send post vars files!? have uncommented line post data query build, don't know how put in request!? :(

index.php

$post_vars = [     'label' => 'description of upload' ];  $files = [     'upload_file_1.txt',     'upload_file_2.txt' ];  try{     $boundary = sha1(1);     $crlf = "\r\n";     $body = '';      foreach($files $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  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 upload' ];  $files = [     'upload_file_1.txt',     'upload_file_2.txt' ];  try{     $boundary = sha1(1);     $crlf = "\r\n";     $body = '';      foreach($post_vars $key => $value){         $body .= '--'.$boundary.$crlf             .'content-disposition: form-data; name="'.$key.'"'.$crlf             .'content-length: '.strlen($value).$crlf.$crlf             .$value.$crlf;     }      foreach($files $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 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  array ( ) post array (     [label] => description of 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 -------------------- 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -