I am trying to use the cURL progress_callback
function, and I'm having a problem with that.
At the begining the function returns 0
for the "loaded" bytes, and 0
for the "total". After that it returns a lot of times: total: the size (it's ok), but it says that the "loaded" is also the file size.
For example, I run the script on a file that his size is 10028
bytes. The result:
{loaded: 0, total: 0}
{loaded: 10028, total: 10028}
{loaded: 10028, total: 10028}
{loaded: 10028, total: 10028}
As you can see, it says "loaded" 10028 and keeps running. I want it to write the real proccess (for instance: if the file size is 10
bytes, so: 0, 3, 6, 9, 10. And not 0, 10, 10, 10, 10)
The code:
$fp = fopen($temp_file, "w");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOPROGRESS, false );
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, "curl_progress_callback");
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
function curl_progress_callback ($download_size, $downloaded_size, $upload_size, $uploaded_size) {
global $fileinfo;
if (!$downloaded_size) {
if (!isset($fileinfo->size)) {
$fileinfo->size = $download_size;
event_callback(array("send" => $fileinfo));
}
}
event_callback(array("progress" => array("loaded" => $downloaded_size, "total" => $download_size)));
}
function event_callback ($message) {
global $callback;
echo "<script>parent.".$callback."(".json_encode($message).");</script>";
}
Thank you!