...

View Full Version : How do i use php to start a file download ?



joshuadavid2007
08-22-2007, 08:13 AM
what functions to use ?

Pennimus
08-22-2007, 11:31 AM
Here's how i do it for PDF's, modify to suit your needs.



header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="filename.pdf"');
readfile('filename.pdf');

aedrin
08-22-2007, 05:03 PM
Read the comments at http://us2.php.net/header

It should have all the information you will need.

hardnrg
08-23-2007, 02:13 AM
I have some code at hand.

Note, this is only SOME of the code you could use;

$path is something like "files/readme.pdf"


$file_extension = strtolower(substr(strrchr($filename,"."),1));

$file = basename($path);
$size = filesize($path);

switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe":
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/octet-stream";
}

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=".basename($file).";" );
header("Content-Length: $size");
header("Content-Transfer-Encoding: binary");

readfile($path);

michaelespinosa
09-07-2007, 07:13 PM
Here is what I use


<?php

$filename = $_GET['file'];

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if( $filename == "" )
{
echo "<html><title>Title Here</title><body>ERROR: download file NOT SPECIFIED. </body></html>";
exit;
} elseif ( ! file_exists( $filename ) )
{
echo "<html><title>Title Here</title><body>ERROR: File not found.</body></html>";
exit;
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

?>

And then when you want to a link to download a file you would use


<a href="http://www.domain.com/download.php?file=filename.pdf">download pdf</a>

And make sure this file is saved as download.php and in the same directory as the file you want to download.

This is just what I use if anyone has something better or a way to improve it let me know.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum