title: How to POST a file with PHP cURL with progress
body: <p>I'm currently running a personal file upload site, which roughly does the following;</p>
  
  <ol>
  <li>Select files to upload through the website</li>
  <li>PHP uploads and zips the files together</li>
  <li>The zipped file gets sent via cURL to a remote storage server</li>
  <li>A URL is presented to share the files</li>
  </ol>
  
  <p>I'm having an issue with step 3. I currently have a progress bar while the file uploads to the website and I'd like another percentage to show the cURL transfer to the storage server. I've seen that you that can use CURLOPT_PROGRESSFUNCTION - but I can not get it working, after a lot of searching.</p>
  
  <p>My current code is as below;</p>
  
  <pre><code>// Prepare the POST data
  $data = array(
      'file' =&gt; new CurlFile('/path/to/local.zip', 'application/zip', 'uploaded_archive.zip'),
      'upload_folder' =&gt; 'folder_name'
  );
  
  // Send the POST
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://example.com/upload.php");
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_NOPROGRESS, false);
  curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progressCall');
  curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 160);
  curl_setopt($ch, CURLOPT_TIMEOUT, 160);
  $res = curl_exec($ch);
  curl_close($ch);
  </code></pre>
  
  <p>This calls "progressCall" with the progress, which looks like the below to test;</p>
  
  <pre><code>// Function to save the output of progress from CURLOPT_PROGRESSFUNCTION
  function progressCall($ch, $dltotal, $dlnow, $uptotal, $upnow) {
      $progress = "$ch/$dltotal/$dlnow/$uptotal/$upnow";
      file_put_contents('tmp/curlupload.txt', 'PERC: ' . $progress . "\n", FILE_APPEND);
  }
  </code></pre>
  
  <p>The problem is, I get the output below;</p>
  
  <p><a href="https://gist.github.com/ialexpw/6160e32495d857cc0d8984373f3808ae" rel="noreferrer">https://gist.github.com/ialexpw/6160e32495d857cc0d8984373f3808ae</a></p>
  
  <pre><code>PERC: Resource id #15/0/0/0/65532
  PERC: Resource id #15/0/0/0/65532
  PERC: Resource id #15/0/0/0/131064
  PERC: Resource id #15/0/0/0/131064
  PERC: Resource id #15/0/0/0/196596
  </code></pre>
  
  <p>This seems to show the uploaded amount going up (which is OK), but has 0 for the total upload amount. I've tried a lot of example online, but I can not work out why the total amount to be uploaded is always 0. I tried setting a Content-Length, although it's the same.</p>
  
  <p>Any help would be appreciated if something is wrong with the above.</p>
  
  <p>Thanks!</p>