title: Upload multiple files and post vars with fsockopen
body: <p>I want both to send multiple files and post vars in a http request with fsockopen..</p>
  
  <p>Have come up with this code, but I don't know how to send the post vars with the files!? Have uncommented the line where the post data query is build, but don't know how to put it in the request!? :(</p>
  
  <h1>index.php</h1>
  
  <pre><code>$post_vars = [
      'label' =&gt; '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-&gt;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-&gt;getMessage();
  }
  </code></pre>
  
  <h1>post.php</h1>
  
  <pre><code>echo "get\n";
  print_r($_GET);
  
  echo "post\n";
  print_r($_POST);
  
  echo "files\n";
  print_r($_FILES);
  </code></pre>
  
  <h1>output</h1>
  
  <pre><code>-------------------- 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] =&gt; Array
          (
              [name] =&gt; Array
                  (
                      [0] =&gt; upload_file_1.txt
                      [1] =&gt; upload_file_2.txt
                  )
  
              [type] =&gt; Array
                  (
                      [0] =&gt; text/plain
                      [1] =&gt; text/plain
                  )
  
              [tmp_name] =&gt; Array
                  (
                      [0] =&gt; C:\wamp\tmp\php1827.tmp
                      [1] =&gt; C:\wamp\tmp\php1828.tmp
                  )
  
              [error] =&gt; Array
                  (
                      [0] =&gt; 0
                      [1] =&gt; 0
                  )
  
              [size] =&gt; Array
                  (
                      [0] =&gt; 18
                      [1] =&gt; 18
                  )
  
          )
  
  )
  
  -------------------- RESPONSE END --------------------
  </code></pre>