| 
	
 | 
 Posted by J.O. Aho on 07/31/06 14:18 
Lupus Yonderboy wrote: 
> Having the need to make some videos available for download from my website,  
> some time ago I created a script by putting together some snippets from  
> different examples found on the net (the first one was from php.net function  
> reference), until everything appeared to run smoothly. The file types I  
> wanted to release through the download function are .avi, .wmv, .mov and  
> .mp4. For some time everything went peachy, until recently I uploaded  
> another video and I noticed that my script doesn't work with files above a  
> given filesize, say around 20 Mb: Firefox opens the small download window on  
> the bottom right and it closes right away, saving a 0 byte file; IE6 does  
> just the same, besides it names the file wrongly (with the name of the .php  
> file containing the call to the download script). Can anybody provide an  
> hint? The script follows: 
 
in your php.ini you limit the amount of memory that a php script can use, this  
limits the size of things for you, say around 20Mb in your case. 
 
 
> <?php 
> function getfile($name, $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 "mov": $ctype="video/quicktime"; break; 
>      case "avi": $ctype="video/x-msvideo"; break; 
>      case "wmv": $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: must-revalidate, post-check=0, pre-check=0"); 
>    header("Cache-Control: public"); 
>    header("Content-Description: File Transfer"); 
>  
>    //Use the switch-generated Content-Type 
>    header("Content-Type: $ctype"); 
>  
>    //Force the download 
>    $header="Content-Disposition: attachment; filename=\"$name\";"; 
>    header($header ); 
>    header("Content-Transfer-Encoding: binary"); 
>    header("Content-Length: ".$len); 
>    @readfile($file); 
>    exit; 
> } 
> ?> 
 
I think fpassthru() uses less memory, as long as you don't use  
ob_start()/ob_flush() 
 
For more info, see http://www.php.net/manual/en/function.fpassthru.php 
 
 
  //Aho
 
  
Navigation:
[Reply to this message] 
 |