Over a million developers have joined DZone.

How To Make Files Downloadable With PHP

DZone's Guide to

How To Make Files Downloadable With PHP

Free Resource
In some situations when you’re creating a web page with links to a file like  pdf – xml – jpeg , or other external program file , you may want user’s browser  prompt  to download the file instead of opening it in the browser window or in an external program . There are a few different methods you can do this , my favorite is PHP-based . Start by creating a new file on your server or on your development computer called download.php  and paste the below code into the file .
Complete article : http://tournasdimitrios1.wordpress.com/2012/01/10/how-to-make-files-downloadable-with-php/

// download.php
 if (isset($_GET['file'])) {
//$file = $_GET['file'];
$file = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_ENCODED);
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header("Content-Type: application/force-download");// some browsers need this
header("Content-Disposition: attachment; filename=\"$file\"");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file2));
}else {
header("HTTP/1.0 404 Not Found");
echo "

Error 404: File Not Found:

"; header('Refresh: 5; url=http://localhost/zy3.php'); print 'You will be redirected in 5 seconds'; exit ; } } else { header('Refresh: 5; url=http://localhost/zy3.php'); print '

You you shouldn\'t be here ......

redirection in 5 seconds


Click here to download PDF


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}