Skip to content

Instantly share code, notes, and snippets.

@jhonsore
Created March 18, 2016 13:21
Show Gist options
  • Save jhonsore/67996dc33896c225742f to your computer and use it in GitHub Desktop.
Save jhonsore/67996dc33896c225742f to your computer and use it in GitHub Desktop.
Force download php
<?php
if(is_file($_GET['src'])) {
$file_name = $_GET['src'];
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg':
case 'jpg': $mime = 'image/jpg'; break;
default: $mime = 'application/force-download';
}
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file_name)); // provide file size
header('Connection: close');
readfile($file_name); // push it out
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Force download</title>
</head>
<body>
<a href="download.php?src=pasta/file.pdf">DOWNLOAD</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment