?
Solved

PHP file stream/download useability question

Posted on 2003-03-10
3
Medium Priority
?
338 Views
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");
     fpassthru($fp);

Thanks in advance!
0
Comment
Question by:Jerf
3 Comments
 

Expert Comment

by:laidbak
ID: 8108829
Try this:

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

Accepted Solution

by:
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.

SO
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='';
  $locext=strtolower($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

Regards
0
 

Author Comment

by:Jerf
ID: 8114049
Works great!  Thanks for your help!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

578 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