Advertisement
bug7sec

IDM with PHP CURL

Aug 13th, 2016
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3. function download($url)
  4. {
  5.     $ch = curl_init();
  6.     curl_setopt($ch, CURLOPT_URL, $url);
  7.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  8.     curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');
  9.     curl_setopt($ch, CURLOPT_NOPROGRESS, false);
  10.     curl_setopt($ch, CURLOPT_HEADER, 0);
  11.     curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  12.     $html = curl_exec($ch);
  13.     curl_close($ch);
  14.     return $html;
  15. }
  16.  
  17.  
  18. function callback($jembut,$download_size, $downloaded, $upload_size, $uploaded)
  19. {
  20.     if( $downloaded != 0 ){
  21.         show_status($downloaded, $download_size, 20);
  22.     }
  23. }
  24.  
  25. function show_status($done, $total, $size=30) {
  26.     $total = str_replace("-", "", $total);
  27.     static $start_time;
  28.     if($done > $total) return;
  29.  
  30.     if(empty($start_time)) $start_time=time();
  31.     $now = time();
  32.     $perc=(double)($done/$total);
  33.     $bar=floor($perc*$size);
  34.     $status_bar="\r[";
  35.     $status_bar.=str_repeat("=", $bar);
  36.     if($bar<$size){
  37.         $status_bar.=">";
  38.         $status_bar.=str_repeat(" ", $size-$bar);
  39.     } else {
  40.         $status_bar.="=";
  41.     }
  42.     $disp=number_format($perc*100, 0);
  43.     $status_bar.="] $disp%  ".formatSizeUnits($done)." / ".formatSizeUnits($total);
  44.     $rate = ($now-$start_time)/$done;
  45.     $left = $total - $done;
  46.     $eta = round($rate * $left, 2);
  47.     $elapsed = $now - $start_time;
  48.     $status_bar.= " Menunggu : ".number_format($eta)." detik.  Time left : ".number_format($elapsed)." sec.";
  49.     echo "$status_bar  ";
  50.     flush();
  51.     if($done == $total) {
  52.         echo "\n";
  53.     }
  54. }
  55. function formatSizeUnits($bytes)
  56.     {
  57.         if ($bytes >= 1073741824)
  58.         {
  59.             $bytes = number_format($bytes / 1073741824, 2) . ' GB';
  60.         }
  61.         elseif ($bytes >= 1048576)
  62.         {
  63.             $bytes = number_format($bytes / 1048576, 2) . ' MB';
  64.         }
  65.         elseif ($bytes >= 1024)
  66.         {
  67.             $bytes = number_format($bytes / 1024, 2) . ' kB';
  68.         }
  69.         elseif ($bytes > 1)
  70.         {
  71.             $bytes = $bytes . ' bytes';
  72.         }
  73.         elseif ($bytes == 1)
  74.         {
  75.             $bytes = $bytes . ' byte';
  76.         }
  77.         else
  78.         {
  79.             $bytes = '0 bytes';
  80.         }
  81.  
  82.         return $bytes;
  83. }
  84.  
  85. download("http://google.com");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement