Page 1 of 1

[Q]cURL Download from php and output progress

Posted: Sun Feb 06, 2011 9:36 am
by fishown
Hello every one, =]

Im having a problem retrun the progress of an PHP cURL script, well I just couldn't find how to.

P.S: I do know that I will have to use javascript to present the output.

I wrote the following: (which as i mentioned download file from ftp to the server using cURL functions);

Code: Select all

<?php

	$file_target = "ls-lR.gz";
	$file_source ='ftp://example.co.il/filename.exe';

 		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $file_source);
		curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
		$fp = fopen($file_target, 'w');
  		curl_setopt($ch, CURLOPT_FILE, $fp);
  		curl_exec ($ch);
  
	$output = 'true';
	 if (curl_errno($ch)) {
   		 trigger_error('CURL error: "' . curl_error($ch) . '"', E_USER_WARNING);
    		$output = 'false';
  	}
 		 curl_close ($ch);
	  fclose($fp);

	echo $output;

?>
Thank you =]

Re: [Q]cURL Download from php and output progress

Posted: Sun Feb 06, 2011 6:19 pm
by requinix
Seems fine so far...

Re: [Q]cURL Download from php and output progress

Posted: Sun Feb 06, 2011 7:36 pm
by fishown
The problem was that the "callback"/"Progress" function return nothing.

Any way, I think I solved the problem, more like figured what cause the problem and I would like if some one
will back me up befor im running to install new php version on my server.

I rad the "PHP 5 ChangeLog" document and I found that the callback/progress funtion added or changed in the version 5.3.0.
An as you can guess im running lower version 5.1.6
Version 5.3.0
30-June-2009
[...]
Implemented FR #41712 (curl progress callback: CURLOPT_PROGRESSFUNCTION). (sdteffen[at]gmail[dot].com, Pierre)
[...]
The resone im asking is that my english is not that good, and im not sure that i understand correctly.

Below, The code after the "progress changes":

Code: Select all

<?php
        function curl_progress_callback($a=0,$b=0,$c=0, $d=0)
        {
	       echo "curl_progress_callback($a,$b,$c,$d)\n";
         }

        $file_target = "ls-lR.gz";
        $file_source ='ftp://example.co.il/filename.exe';

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $file_source);
                curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
                $fp = fopen($file_target, 'w');
                curl_setopt($ch, CURLOPT_FILE, $fp);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_NOPROGRESS, false);
                curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'curl_progress_callback');
                curl_exec ($ch);
  
        $output = 'true';
         if (curl_errno($ch)) {
                 trigger_error('CURL error: "' . curl_error($ch) . '"', E_USER_WARNING);
                $output = 'false';
        }
                 curl_close ($ch);
          fclose($fp);

        echo $output;

?>