5

I'm currently running a personal file upload site, which roughly does the following;

  1. Select files to upload through the website
  2. PHP uploads and zips the files together
  3. The zipped file gets sent via cURL to a remote storage server
  4. A URL is presented to share the files

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.

My current code is as below;

// Prepare the POST data
$data = array(
    'file' => new CurlFile('/path/to/local.zip', 'application/zip', 'uploaded_archive.zip'),
    'upload_folder' => '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);

This calls "progressCall" with the progress, which looks like the below to test;

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

The problem is, I get the output below;

https://gist.github.com/ialexpw/6160e32495d857cc0d8984373f3808ae

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

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.

Any help would be appreciated if something is wrong with the above.

Thanks!

ialexpw
  • 51
  • 3
  • Does this answer your question? [In my CURL CURLOPT\_PROGRESSFUNCTION callback, dltotal is always 0](https://stackoverflow.com/questions/22417507/in-my-curl-curlopt-progressfunction-callback-dltotal-is-always-0) – Justinas Jan 14 '20 at 11:41
  • 2
    Unfortunately this is for downloading a file, not uploading so it does it slightly differently with CURLOPT_FILE which I don't think I can use – ialexpw Jan 14 '20 at 11:46
  • Does this answer your question? [cURL download progress in PHP](https://stackoverflow.com/questions/13958303/curl-download-progress-in-php) – Anuga Jan 14 '20 at 12:39
  • 1
    I have tried that also, but that's also for downloading - not uploading as I need. So it's a slightly different format. – ialexpw Jan 14 '20 at 13:43

1 Answers1

0

Hello sorry for the response 2 years after, but just working on it !

Possible for Upload, but as mentionned we don't have in this example the total file size.

The easiest way if possible is to take the file size on the callback function, with a global for example :

function progressCall($ch, $dltotal, $dlnow, $uptotal, $upnow) {
    // You will get this before with | $fsize = filesize($thefile);
    global $fsize;
    
    $percent = round($upnow / $fsize * 100);
    echo $percent . ' % downloaded';
}
Antibug66
  • 11
  • 1