[英]Php header force download - doesn't work
Hi I have this code but not happens, any idea? 嗨,我有这段代码,但是没有发生,知道吗? When I run this page, I got a blank page, one thing that I don't understand is the filename is just the name and no the entire address.
当我运行此页面时,我得到了一个空白页面,我不明白的一件事是文件名只是名称而没有整个地址。 A url is like this, with the complete address:
网址是这样的,带有完整的地址:
http://site.com/download_orig.php?file=prod_media/images/neografik_pty_ltd/product_test_10/192027102011_istock_000016116673xsmall.jpg http://site.com/download_orig.php?file=prod_media/images/neografik_pty_ltd/product_test_10/192027102011_istock_000016116673xsmall.jpg
Cheers, M M,干杯
<?php
$filename = $_GET['file'];
if (!is_file($filename)) {
header('Location: home.php'); exit;
}
$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));
switch($extension) {
case "pdf": $mime="application/pdf"; break;
case "rvt": $mime="application/octet-stream"; break;
case "rft": $mime="application/octet-stream"; break;
case "rfa": $mime="application/octet-stream"; break;
case "xls": $mime="application/vnd.ms-excel"; break;
case "xlsx": $mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
case "dwg": $mime="application/acad"; break;
case "dwf": $mime="application/acad"; break;
case "gif": $mime="image/gif"; break;
case "png": $mime="image/png"; break;
case "jpeg":
case "jpg": $mime="image/jpg"; break;
default: $mime=0;
}
// use this unless you want to find the mime type based on extension
// $mime = array('application/octet-stream');
// Only allowed files here, no hack and stuff, sorry
if($mime===0) header('Location: home.php'); exit;
header('Content-Description: File Transfer');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: '.sprintf('%d', $filesize));
// check for IE only headers
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
} else {
header('Pragma: no-cache');
}
ob_clean();
flush();
readfile($filename);
exit;
?>
You have 你有
if($mime===0) header('Location: home.php'); exit;
exit is always called (even if $mime is not zero) causing program to terminate. 总是调用exit(即使$ mime不为零)也会导致程序终止。 Do this:
做这个:
if($mime===0) { header('Location: home.php'); exit; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.