Downloading Files
Using the header function to force file downloads
If you have files that you would like users to be able to download, it can be done simply by providing a link to the file. When the link is clicked, the browser will prompt the user to download the file or it will display it if the file type is known to the browser i.e. text files and gif/jpeg images. If you do not want the files displayed in the browser you can use the header() in PHP with the appropriate content type to force a file download.
This particular script is coded to receive the name of the file to be downloaded via the URL.
PHP Code:
<?php
// downloading a file
$filename = $_GET['filename'];
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");
/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>Take a look at the HTTP/1.1 specification for more information on HTTP headers.
Useful tips on downloading files from the PHP manual:
"There is a bug in Microsoft Internet Explorer 4.01 that prevents this from working. There is no workaround. There is also a bug in Microsoft Internet Explorer 5.5 that interferes with this, which can be resolved by upgrading to Service Pack 2 or later.
For Internet Explorer, header("Content-Disposition: attachment") should be used so that the file contents will not be displayed (be inline) by the browser.
For Netscape, header("Content-Type: application/octet-stream").
Another general fix for many of the problems that occure when trying to prompt a filename to download is to append a '/' to your download URL. This fixes a NS7 problem where it wants to add a .PHP extension to all filenames.
For example, change:
"http://www.url.com/download.php?file=23" to "http://www.url.com/download.php/?file=23"
- PHP manual: Header function
It is a good idea to specify the directory that you want users to be able to download files from and to implement error or security checking. You could check to make sure that a filename has been specified or the user has permission to download a file etc.
Note: I only tested this script using IE 5.5 SP2 on a PC.
See it in action. ¦ Complete code