FORUMS: list search recent posts

Download function PHP

COW Forums : Web Design

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Rachel Broomfield
Download function PHP
on Dec 11, 2010 at 9:28:04 pm

If anyone could help, I would greatly appreciate it. I am trying to create a "download" function on my site. The visitor would be able to click on the "download" key and print the form.

This is what I have:


// place this code inside a php file and call it f.e. "download.php"
$path = $_SERVER['DOCUMENT_ROOT']."/path2file/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_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 'attachment' to force a download
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);
// example: place this kind of link into the document where the file download is offered:
// Download here

Return to posts index

Mike Smith
Re: Download function PHP
on Dec 12, 2010 at 10:32:25 am

You know that you could simply use a link to your document, unless you are dynamically generating different versions of the file for each person who will download it ?

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 All Rights Reserved