PHP file stream/download useability question

Posted on 2003-03-10
Medium Priority
Last Modified: 2006-11-17
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");

Thanks in advance!
Question by:Jerf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8108829
Try this:

$filename = "wordfile.doc";
header('Content-Type: application/msword');
LVL 15

Accepted Solution

VGR earned 1000 total points
ID: 8109428
I've a better solution.
It happens that there is a MIME-Type forcing the "save as" dialog when sent with data.

1) if (***if***) you want your users to are forced to download locally the file, you may use this technique. The MIME-Type to send is "application/save"

2) If you would prefer to have the files served in situ from the webserver, then you have to sent out the CORRECT MIME-Type for the document served. This implies that the MIME-Type descriptor (like 'application/msword') is stored in the database, side-by-side with the filename or actual data.

If you don't store the MIME-Type with the filename, you can try to deduce it from the extension using a function like this :
 // détermination du type de l'attachement
  // calcul extension du nom local
  if (strpos($filename,'.')) $locext=substr($filename,strpos($filename,'.')); // . dedans
  else $locext='';
  switch ($locext) {
    case '.jpg' :
    case '.jpe' :
    case '.jpeg' : $locmimetype='image/jpeg'; $locencode='base64'; break;
    case '.txt' : $locmimetype='text/plain'; $locencode="7bit"; break;
    case '.ai' :
    case '.eps' :
    case '.ps' : $locmimetype='application/postscript'; $locencode="7bit"; break;
    case '.rtf' : $locmimetype='application/rtf'; $locencode="7bit"; break;
    case '.wav' : $locmimetype='audio/x-wav'; $locencode="base64"; break;
    case '.gif' : $locmimetype='image/gif'; $locencode="base64"; break;
    case '.tiff' :
    case '.tif' : $locmimetype='image/tiff'; $locencode="base64"; break;
    case '.html' : $locmimetype='text/html'; $locencode="7bit"; break;
    case '.mpeg' :
    case '.mpg' :
    case '.mpe' : $locmimetype='video/mpeg'; $locencode="base64"; break;
    case '.mov' : $locmimetype='video/quicktime'; $locencode="base64"; break;
    case '.avi' : $locmimetype='video/x-msvideo'; $locencode="base64"; break;
    case '.doc' : $locmimetype='application/msword'; $locencode="base64"; break;
    case '.pdf' : $locmimetype='application/pdf'; $locencode="base64"; break;
    default : $locmimetype='text/plain'; $locencode="7bit"; break;
  } // case of $locext


Author Comment

ID: 8114049
Works great!  Thanks for your help!

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question