note 57243 modified in function.fread by danbrown

From: Date: Fri, 06 Jan 2012 21:09:56 +0000
Subject: note 57243 modified in function.fread by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-185434@lists.php.net to get a copy of this message
i fixed the resume download function since there were few bugs in it. here it goes changes: - i added "partial content" header - added "bytes" to the first content-range header - changed _ENV to _SERVER on HTTP_RANGE best regards from xjust. <?php function dl_file_resume($file){ //First, see if the file exists if (!is_file($file)) { die("<b>404 File not found!</b>"); } //Gather relevent info about file $len = filesize($file); $filename = basename($file); $file_extension = strtolower(substr(strrchr($filename,"."),1)); //This will set the Content-Type to the appropriate setting for the file switch( $file_extension ) { case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "mp3": $ctype="audio/mpeg"; break; case "mpg":$ctype="video/mpeg"; break; case "avi": $ctype="video/x-msvideo"; break; //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files) case "php": case "htm": case "html": case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break; default: $ctype="application/force-download"; } //Begin writing headers // header("Pragma: public"); // header("Expires: 0"); header("Cache-Control:"); header("Cache-Control: public"); // header("Content-Description: File Transfer"); //Use the switch-generated Content-Type header("Content-Type: $ctype"); $filespaces = str_replace("_", " ", $filename); //if your filename contains underscores, you can replace them with spaces // $header='Content-Disposition: attachment; filename='.$filespaces; header($header ); // header("Accept-Ranges: bytes"); // header("Content-Transfer-Encoding: binary"); $size=filesize($file); //check if http_range is sent by browser (or download manager) if(isset($_SERVER['HTTP_RANGE'])) { list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); //if yes, download missing part str_replace($range, "-", $range); $size2=$size-1; $new_length=$size2-$range; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range$size2/$size"); } else { $size2=$size-1; header("Content-Range: bytes 0-$size2/$size"); header("Content-Length: ".$size2); } //open the file $fp=fopen("$file","r"); //seek to start of missing part fseek($fp,$range); //start buffered download while(!feof($fp)) { //reset time limit for big files set_time_limit(0); print(fread($fp,1024*8)); flush(); } fclose($fp); exit; } ?> --was-- i fixed the resume download function since there were few bugs in it. here it goes changes: - i added "partial content" header - added "bytes" to the first content-range header - changed _ENV to _SERVER on HTTP_RANGE best regards from xjust. function dl_file_resume($file){ //First, see if the file exists if (!is_file($file)) { die("<b>404 File not found!</b>"); } //Gather relevent info about file $len = filesize($file); $filename = basename($file); $file_extension = strtolower(substr(strrchr($filename,"."),1)); //This will set the Content-Type to the appropriate setting for the file switch( $file_extension ) { case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "mp3": $ctype="audio/mpeg"; break; case "mpg":$ctype="video/mpeg"; break; case "avi": $ctype="video/x-msvideo"; break; //The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files) case "php": case "htm": case "html": case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break; default: $ctype="application/force-download"; } //Begin writing headers // header("Pragma: public"); // header("Expires: 0"); header("Cache-Control:"); header("Cache-Control: public"); // header("Content-Description: File Transfer"); //Use the switch-generated Content-Type header("Content-Type: $ctype"); $filespaces = str_replace("_", " ", $filename); //if your filename contains underscores, you can replace them with spaces // $header='Content-Disposition: attachment; filename='.$filespaces; header($header ); // header("Accept-Ranges: bytes"); // header("Content-Transfer-Encoding: binary"); $size=filesize($file); //check if http_range is sent by browser (or download manager) if(isset($_SERVER['HTTP_RANGE'])) { list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']); //if yes, download missing part str_replace($range, "-", $range); $size2=$size-1; $new_length=$size2-$range; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range$size2/$size"); } else { $size2=$size-1; header("Content-Range: bytes 0-$size2/$size"); header("Content-Length: ".$size2); } //open the file $fp=fopen("$file","r"); //seek to start of missing part fseek($fp,$range); //start buffered download while(!feof($fp)) { //reset time limit for big files set_time_limit(0); print(fread($fp,1024*8)); flush(); } fclose($fp); exit; } http://php.net/manual/en/function.fread.php

« previous php.notes (#185434) next »