嗨,我有这个代码,但没有发生,任何想法? 当我运行这个页面时,我得到一个空白页面,我不明白的一件事是文件名只是名称而没有整个地址。 网址是这样的,地址完整:
干杯,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;
?>
答案 0 :(得分:1)
你有
if($mime===0) header('Location: home.php'); exit;
exit(即使$ mime不为零)导致程序终止。这样做:
if($mime===0) { header('Location: home.php'); exit; }