Status
Not open for further replies.

saninokia

Active Member
929
2010
71
15
i need a progress bar for my downloader script,my script is curl based and its saving files using cul file function,and i don't have knowledge about progressbar,guys please help me.i need it urgent.

What I Need?
Ans: every 5 sec it will update the status of trasfer,like how much downloaded in 5 sec/total size and then status of 10 sec count......or in %​
 
20 comments
Getting size of whole file:
PHP:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.dsa.da/urfile.rar");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
$info = curl_getinfo($ch);
$maxSize = $info['download_content_length'];
$info is an arrays. print_r it so you can check what it contains.

Upadaing every 5 sec i did in JS, its just ajax request of file:
PHP:
echo filesize("urfile.rar")

That ajax request update value of html5 tag:
Code:
<progress max="<?php echo $maxSize ?>" value=""></progress>

I didnt post my whole scripts because its part of something bigger. Anyway hope it help :)
 
yep like halcyon, and me before said. do that in javascript
things you need:
Code:
setTimeout(function() {
// here your ajax request
},5000);
and about request - google that if u dont know:
jQuery.ajax()
 
curl progress info example php function
you can use this progress function in any curl php script just add

if $ch = curl_init($link);

add this line after $ch = curl_init($link);

example 1
PHP:
            $ch = curl_init($link);
        addProgressInfo($ch);

OR if $c = curl_init();

example 2
PHP:
            $c = curl_init();
        addProgressInfo($c);


Full function with example
PHP:
        function Host($link, $postfields = '', $cookie = '') {
            $ch = curl_init($link);
        addProgressInfo($ch);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_REFERER, 'http://www.filehosts.com/');
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
            if ($postfields) {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
            }
            if ($cookie) {
                curl_setopt($ch, CURLOPT_COOKIE, $cookie); 
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
            }
            $page = curl_exec($ch);
            return($page);
            curl_close($ch);
        } 


function progress_info($dltotal,$dlnow,$ultotal,$ulnow){
                @passthru('clear');
                if($ultotal>0){
                        echo sprintf("    Upload: %.2f%% of %.2f MB\n"
                                ,($ulnow/$ultotal)*100
                                ,$ultotal/(1024*1024)
                        );
                }
                if($dltotal>0){
                        echo sprintf("  Download: %.2f%% of %.2f MB\n"
                                ,($dlnow/$dltotal)*100
                                ,$dltotal/(1024*1024)
                        );
                }
                return(0);
        }


        function addProgressInfo($ch){
                curl_setopt($ch, CURLOPT_NOPROGRESS, false);
            curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress_info');
        }
 
i am using this code for save a file,how to add progressbar in it? i tried with adding your progressbar code but its not working.
PHP:
function curldlfile($link, $saveto)
{
    $handle = fopen($saveto, 'w');
    if ($handle)
    {
        $ch = curl_init( $link);
        curl_setopt($ch, CURLOPT_HEADER, 1); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_FILE, $handle);
 	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
        curl_exec($ch);
        curl_close($ch);
        fclose($handle);
    }
    else
    {
        echo "<br />Change DownLoad To Folder to 777?";
        exit();
    }
}
 
i have add progress bar in your filesonic function

SCREENSHOT
[SLIDE]http://i.lulzimg.com/01a4c4195d.jpg[/SLIDE]


PHP:
<?php  
$filespath = "/folder/";
$link = "http://www.filesonic.com/file/WbJclvs"; 

$filename = getfsofilename($link); 
$link = getfsolink($link); 
$saveto = $filespath . $filename; 
echo "<b>Downloading</b> ..........</br><b>Filename:</b> $filename</br><b>Link:</b> $link ....<br />"; 
curldlfile($link, $saveto);

function getfsofilename($link) 
{ 
    $user = "fsemail"; // 
    $pass = "fspassword"; // 
    $page = $link; 
    $id = explode("/", $page); 
    $id = trim($id[4]); 
    $apicall = "http://api.filesonic.com/link?method=getDownloadLink&u=$user&p=$pass&ids=$id"; 
    $page = file_get_contents($apicall); 
    preg_match('#\"filename\":\"(.*)\",\"url\"#', $page, $match1); 
    $filename = $match1[1]; 
    return $filename; 
}

function getfsolink($link) 
{ 
    $user = "fsemail"; // 
    $pass = "fspassword"; // 
    $page = $link; 
    $id = explode("/", $page); 
    $id = trim($id[4]); 
    $apicall = "http://api.filesonic.com/link?method=getDownloadLink&u=$user&p=$pass&ids=$id"; 
    $page = file_get_contents($apicall); 
    preg_match('#\"url\":\"(.*)\"}}},#', $page, $match); 
    $linksid = $match[1]; 
    $link = str_replace("\/","/",$linksid); 
    return $link; 
}

function curldlfile($link, $saveto) 
{ 
    $handle = fopen($saveto, 'w'); 
    if ($handle) 
    { 
        $ch = curl_init(); 
        addProgressInfo($ch); 
        curl_setopt($ch, CURLOPT_URL, $link); 
        curl_setopt($ch, CURLOPT_FILE, $handle); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'); 
        curl_exec($ch); 
        curl_close($ch); 
        fclose($handle); 
    } 
    else 
    { 
        echo "<br />Could not download $link. Is your 'files' folder chmodded to 777?"; 
        exit(); 
    } 
}

function progress_info($dltotal,$dlnow,$ultotal,$ulnow){
                @passthru('clear');
                if($ultotal>0){
                        echo sprintf("    Upload: %.2f%% of %.2f MB\n"
                                ,($ulnow/$ultotal)*100
                                ,$ultotal/(1024*1024)
                        );
                }
                if($dltotal>0){
                        echo sprintf("  Download: %.2f%% of %.2f MB\n"
                                ,($dlnow/$dltotal)*100
                                ,$dltotal/(1024*1024)
                        );
                }
                return(0);
        }


        function addProgressInfo($ch){
                curl_setopt($ch, CURLOPT_NOPROGRESS, false);
            curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress_info');
        }  
?>
 
getting a problem,its not updating in one line,can help plz?


f76ad9940a.png
 
Status
Not open for further replies.
Back
Top