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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Galt
Forum Newbie
Posts: 18
Joined: Thu May 22, 2003 8:08 am

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

Post 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.
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post 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.
Post Reply