Page 1 of 1

How to specify filename in header() method with content-type

Posted: Fri Jun 06, 2003 11:29 am
by Galt
This is a file I use to let users download a file stored elsewhere in the system:

Code: Select all

<?
//this is file getfile.php

$filename=$_GET['file'];
if(file_exists($filename)){
  header("content-type: application/zip");
  readfile($filename);
}
?>
This does not seem to work on IE because the browser doesnt know what to save the filename as. It says "saving getfile.php" and then says it can't, and stops. However, Mozilla 1.3 in linux saves the file (but its named getfile.php). If the saved getfile.php is renamed to blah.zip, then it can be successfully unzipped and it has all the correct contents.

My question is, how do I tell the browser what to name the savefile as? I'm thinking the answer lies in something along these lines:

Code: Select all

header("content-type: application/zip, content-name: ".$filename);
So that not only does the browser know what the file type is, but also what a suggested save fiel name is.

Posted: Fri Jun 06, 2003 11:47 am
by Galahad
Read this thread. Kim posted some good code, and later on I link to a tutorial I found helpful. I also describe more of my system to prevent users from accessing files directly. I think it's a pretty good system.