Link to home
Start Free TrialLog in
Avatar of Jerf
Jerf

asked on

PHP file stream/download useability question

I've worked on a website that does a lot of file handling throughout it.  There are a number of places throughout the website where I need to serve files out to someone who clicks a link.  These files need to be secure and are therefore hidden off of the webserver.

Many of the website users aren't web savvy and when the file open dialog box pops up, the simply click 'open', but the files don't work.  What usually happens (most of these documents are Word, excel or other MS Office docs) is the program loads, and then an error such as "This file cannot be opened" is displayed.  If the user saves the file to his/her drive then runs the file, it works fine.

This is confusing the website's users to no end and I've been asked to do something about it.

Does anyone know why this does this?  Is there a way to fix it?

This is my code for downloading a file:

        $filename = $docinfo['filename'];
     $file_path = $docinfo['path'];
     $file = $file_path."/".$filename;
     $len = filesize($file);
     header("content-type: application/stream");
     header("content-length: $len");
     header("content-disposition: attachment; filename=$filename");
     $fp=fopen($file, "r");
     fpassthru($fp);

Thanks in advance!
Avatar of laidbak
laidbak

Try this:

<?
$filename = "wordfile.doc";
header('Content-Type: application/msword');
readfile($filename);
?>
ASKER CERTIFIED SOLUTION
Avatar of VGR
VGR

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jerf

ASKER

Works great!  Thanks for your help!