cURL download progress in PHP
What you need is
<?php
ob_start();
echo "<pre>";
echo "Loading ...";
ob_flush();
flush();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);
function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
if($download_size > 0)
echo $downloaded / $download_size * 100;
ob_flush();
flush();
sleep(1); // just to see effect
}
echo "Done";
ob_flush();
flush();
?>
PHP/Curl progress while downloading
change the php str_replace to
str_replace(['#', ' '], '', $b);
Then you get only the percentage, without the preceding blanks, which you can just insert to the container without editing.
Example:
https://3v4l.org/OGWqU
php cURL progress function size limit
CURLOPT_PROGRESSFUNCTION function argument type are double, so you are probably casting these value to int somewhere in your code. and probably since your os is 32bit based architecture you are overflowing the int size limit. I would suggest you revise your code to avoid casting to int big double numbers.
cURL Download Progress in PHP not working?
There doesn't seem to be a CURLOPT_PROGRESSFUNCTION before php 5.3.
Take a look at http://cvs.php.net/viewvc.cgi/php-src/ext/curl/interface.c?view=log and you will find two entries - [DOC] MFH: #41712, implement progress callback
. One for the php5.3 and one for the php6 branch.
edit: With php 5.2.x you should be able to set a stream_notification_callback
function foo() {
$args = func_get_args();
echo join(', ', $args), "\n";
}
$ctx = stream_context_create(null, array('notification' =>'foo'));
$fpIn = fopen('http://php.net/', 'rb', false, $ctx);
file_put_contents('localfile.txt', $fpIn);
Curl progress function in PHP
Declare your function progress
outside of download
:
function download($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$html = curl_exec($ch);
curl_close($ch);
}
function progress($resource, $download_size, $downloaded, $upload_size, $uploaded)
{
if ($download_size > 0)
$progress = round($downloaded / $download_size * 100);
$progress = array('progress' => $progress);
$path = "temp/";
$destination = $path."11.json";
$file = fopen($destination, "w+");
fwrite($file, json_encode($progress, JSON_UNESCAPED_UNICODE));
fclose($file);
}
Related Topics
How to Read Only 5 Last Line of the Text File in PHP
Serve Image with PHP Script VS Direct Loading an Image
How to Generate Random Date Between Two Dates Using PHP
Generating Xml Document in PHP (Escape Characters)
How to Get Email Address from a Long String
Using PHP Variables Inside HTML Tags
Date Function Output in a Local Language
File_Get_Contents - Failed to Open Stream: Http Request Failed! Http/1.1 404 Not Found
Preventing Caching of CSS Files
Apache Rewrite - Get Original Url in PHP
Laravel 5.3 Auth Check in Constructor Returning False
How to Write Data into an Excel Using PHP
How to Display Image from Database Using PHP
How to Capitalize First Letter of First Word in a Sentence