Page 1 of 1

Struggling with file download script in PHP

Posted: Sun May 23, 2010 4:36 pm
by dc2000
Hi everyone:


I've spent probably about an hour trying to fix this issue. My goal is to let people download a short WMV file. If I simply post a link, it automatically goes into Media Player and not the download screen. So I came up with the following script to have a download window pop up instead:

Code: Select all

//$fpath = Path of the file on the server 
//$download_file_name = is the name of file, how it will appear for a user 

$len = @filesize($fpath); 
         
header("Content-Type: application/x-video-wmv"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".$len); 
header("Content-disposition: attachment; filename=\"$download_file_name\""); 

if(@readfile($fpath) === false) 
    ShowError("ERROR: Cannot output the file, please contact administrator...");  
Everything works fine, except that when a download begins both IE and Firefox don't show a download progress bar, and instead say, "Estimated time left not known". I checked the file size and it is reported correctly. What am I doing wrong?

Re: Struggling with file download script in PHP

Posted: Mon May 24, 2010 1:08 am
by Christopher
Take the @ off the readfile() to see the error. I would recommend doing a file_exists() check first. If it passes then send headers and call readfile() otherwise show the error (with normal HTML headers).