This is my favourite download-script, it works with files bigger than 10 MB on Apache 1.3 (or higher). I added the cache control to open also text files and others (without saving). Try the demo.
By : olaf
<?php // ver. 1.04 - added an example how to use this script with the filename from a query string // ver. 1.03 - moved the disposition header into the switch because the attachement attibute is sometimes needed // ver. 1.02 - use pathinfo() to get the fileinformation (extesnion) now // ver. 1.01 - Added the stringconv. to handle also extensions with caps
// place this code inside a php file and call it f.e. "download.php" $path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // play with the path if the document root does noet exist $fullPath = $path.$_GET['downnload_file'];
if ($fd = fopen ($fullPath, "r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); switch ($ext) { case "pdf": header("Content-type: application/pdf"); // add here more headers for diff. extensions header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachement' to force a download break; default; header("Content-type: application/octet-stream"); header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); } header("Content-length: $fsize"); header("Cache-control: private"); //use this to open files directly while(!feof($fd)) { $buffer = fread($fd, 2048); echo $buffer; } } fclose ($fd); exit; // example: place this kind of link into your document where the download is shown:
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.