thieumao

copyfile_chunked - download big file with PHP

Feb 26th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Copy remote file over HTTP one small chunk at a time.
  4.  *
  5.  * @param $infile The full URL to the remote file
  6.  * @param $outfile The path where to save the file
  7.  */
  8. function copyfile_chunked($infile, $outfile) {
  9.     $chunksize = 10 * (1024 * 1024); // 10 Megs
  10.  
  11.     /**
  12.      * parse_url breaks a part a URL into it's parts, i.e. host, path,
  13.      * query string, etc.
  14.      */
  15.     $parts = parse_url($infile);
  16.     $i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
  17.     $o_handle = fopen($outfile, 'wb');
  18.  
  19.     if ($i_handle == false || $o_handle == false) {
  20.         return false;
  21.     }
  22.  
  23.     if (!empty($parts['query'])) {
  24.         $parts['path'] .= '?' . $parts['query'];
  25.     }
  26.  
  27.     /**
  28.      * Send the request to the server for the file
  29.      */
  30.     $request = "GET {$parts['path']} HTTP/1.1\r\n";
  31.     $request .= "Host: {$parts['host']}\r\n";
  32.     $request .= "User-Agent: Mozilla/5.0\r\n";
  33.     $request .= "Keep-Alive: 115\r\n";
  34.     $request .= "Connection: keep-alive\r\n\r\n";
  35.     fwrite($i_handle, $request);
  36.  
  37.     /**
  38.      * Now read the headers from the remote server. We'll need
  39.      * to get the content length.
  40.      */
  41.     $headers = array();
  42.     while(!feof($i_handle)) {
  43.         $line = fgets($i_handle);
  44.         if ($line == "\r\n") break;
  45.         $headers[] = $line;
  46.     }
  47.  
  48.     /**
  49.      * Look for the Content-Length header, and get the size
  50.      * of the remote file.
  51.      */
  52.     $length = 0;
  53.     foreach($headers as $header) {
  54.         if (stripos($header, 'Content-Length:') === 0) {
  55.             $length = (int)str_replace('Content-Length: ', '', $header);
  56.             break;
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * Start reading in the remote file, and writing it to the
  62.      * local file one chunk at a time.
  63.      */
  64.     $cnt = 0;
  65.     while(!feof($i_handle)) {
  66.         $buf = '';
  67.         $buf = fread($i_handle, $chunksize);
  68.         $bytes = fwrite($o_handle, $buf);
  69.         if ($bytes == false) {
  70.             return false;
  71.         }
  72.         $cnt += $bytes;
  73.  
  74.         /**
  75.          * We're done reading when we've reached the conent length
  76.          */
  77.         if ($cnt >= $length) break;
  78.     }
  79.  
  80.     fclose($i_handle);
  81.     fclose($o_handle);
  82.     return $cnt;
  83. }
  84.  
  85. $infile = "http://cs532205v4.vk.me/u190923389/videos/e996ca0dbf.360.mp4";
  86. $outfile = "e996ca0dbf.360.mp4";
  87. copyfile_chunked($infile, $outfile);
  88. echo "Thanh cong";
Add Comment
Please, Sign In to add comment