Multiple file upload from archive supporting RANGE requests

    Sometimes it is useful not to save a bunch of files on any hosting, but to use only one archive file. But there is a problem of access to a specific file in the archive. I bring to your attention a PHP script that allows you to download part of the archive file as a whole file. Supports reloading through RANGE requests. An archive, in this case, is simply a gluing of many files. The script is currently tested with the Flashget download manager.

    In order to download a specific file, it is enough to indicate its offset in the archive file, the size and name of this file as follows:

    htp: //address/script_name.php? Name = filename & offset = 3000 & size = 100

    As a result, the user of such a link will receive a file of 100 bytes in size named filename, which is located at an offset of 3000 archive file.

    Copy Source | Copy HTML
    1. $offset = isset($_REQUEST['offset']) ? (int) $_REQUEST['offset'] : 0;
    2. $size = isset($_REQUEST['size']) ? (int) $_REQUEST['size'] : false;
    3. $name = isset($_REQUEST['name']) ? urlencode($_REQUEST['name']) : 'download.bin';
    4.  
    5. if (empty($size)) die;
    6.  
    7. // link to archive file
    8. $url = 'http://archive_file_url';
    9.  
    10. @set_time_limit(0);
    11.  
    12. $range_size = $size;
    13. $range_offset = $offset;
    14.  
    15. if (isset($_SERVER['HTTP_RANGE'])) {
    16.     if (preg_match('/^bytes=(\d+)\-$/', $_SERVER['HTTP_RANGE'], $m)) {
    17.         $range_offset+=$m[1];
    18.         $range_size-=$m[1];
    19.     } elseif (preg_match('/^bytes=(\d+)\-(\d+)$/',$_SERVER['HTTP_RANGE'], $m)) {
    20.         $range_offset+=$m[1];
    21.         $range_size=$m[2]-$m[1]+1;
    22.     } else {
    23.       // TODO (aig): support other ranges if needed
    24.         die();
    25.     }
    26.     header('HTTP/1.1 206 Partial Content');
    27. } else {
    28.     header('HTTP/1.1 200 Ok');
    29. }
    30.  
    31. $range = ($range_offset)."-".($range_offset+$range_size-1);
    32. header("Content-Type: application/octet-stream");
    33. header("Accept-Ranges: bytes");
    34. header("Content-Length: $range_size");
    35. header('Content-Disposition: attachment; filename="' . $name . '";');
    36.  
    37. $ch = curl_init();
    38. curl_setopt($ch,CURLOPT_URL,$url);
    39. curl_setopt($ch,CURLOPT_RANGE,$range);
    40. curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
    41. curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    42. curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
    43. curl_exec($ch);
    44. curl_close($ch);
    45. ?>
    46.  

    Also popular now: