apache - php resume capability or download in chunks -
php $_server not return http_range key , without key how can perform "resume download" or download in chunks?
if (isset($_server['http_range'])) { echo "find"; } else{ echo "not find"; }
i can't $_server['http_range']
server .......
there no key http_range
when print $_server
===================================================
<?php // file request, throw error if nothing supplied // hide notices @ini_set('error_reporting', e_all & ~ e_notice); //- turn off compression on server @apache_setenv('no-gzip', 1); @ini_set('zlib.output_compression', 'off'); if(!isset($_request['file']) || empty($_request['file'])) { header("http/1.0 400 bad request"); exit; } // sanitize file request, keep name , extension // also, replaces file location preset 1 ('./myfiles/' in example) $file_path = $_request['file']; $path_parts = pathinfo($file_path); $file_name = $path_parts['basename']; $file_ext = $path_parts['extension']; $file_path = './myfiles/' . $file_name; // allow file streamed instead of sent attachment $is_attachment = isset($_request['stream']) ? false : true; // make sure file exists if (is_file($file_path)) { $file_size = filesize($file_path); $file = @fopen($file_path,"rb"); if ($file) { // set headers, prevent caching header("pragma: public"); header("expires: -1"); header("cache-control: public, must-revalidate, post-check=0, pre-check=0"); header("content-disposition: attachment; filename=\"$file_name\""); // set appropriate headers attachment or streamed file if ($is_attachment) { header("content-disposition: attachment; filename=\"$file_name\""); } else { header('content-disposition: inline;'); header('content-transfer-encoding: binary'); } // set mime type based on extension, add yours if needed. $ctype_default = "application/octet-stream"; $content_types = array( "exe" => "application/octet-stream", "zip" => "application/zip", "mp3" => "audio/mpeg", "mpg" => "video/mpeg", "avi" => "video/x-msvideo", ); $ctype = isset($content_types[$file_ext]) ? $content_types[$file_ext] : $ctype_default; header("content-type: " . $ctype); //check if http_range sent browser (or download manager) if(isset($_server['http_range'])) { list($size_unit, $range_orig) = explode('=', $_server['http_range'], 2); if ($size_unit == 'bytes') { //multiple ranges specified @ same time, simplicity serve first range //http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt list($range, $extra_ranges) = explode(',', $range_orig, 2); } else { $range = ''; header('http/1.1 416 requested range not satisfiable'); exit; } } else { $range = ''; } //figure out download piece range (if set) list($seek_start, $seek_end) = explode('-', $range, 2); //set start , end based on range (if set), else set defaults //also check invalid ranges. $seek_end = (empty($seek_end)) ? ($file_size - 1) : min(abs(intval($seek_end)),($file_size - 1)); $seek_start = (empty($seek_start) || $seek_end < abs(intval($seek_start))) ? 0 : max(abs(intval($seek_start)),0); //only send partial content header if downloading piece of file (ie workaround) if ($seek_start > 0 || $seek_end < ($file_size - 1)) { header('http/1.1 206 partial content'); header('content-range: bytes '.$seek_start.'-'.$seek_end.'/'.$file_size); header('content-length: '.($seek_end - $seek_start + 1)); } else header("content-length: $file_size"); header('accept-ranges: bytes'); set_time_limit(0); fseek($file, $seek_start); while(!feof($file)) { print(@fread($file, 1024*8)); ob_flush(); flush(); if (connection_status()!=0) { @fclose($file); exit; } } // file save success @fclose($file); exit; } else { // file couldn't opened header("http/1.0 500 internal server error"); exit; } } else { // file not exist header("http/1.0 404 not found"); exit; } ?>
Comments
Post a Comment