0
안녕하세요,이 코드가 있지만 어떤 생각이 있습니까? 이 페이지를 실행하면 빈 페이지가 나타납니다. 이해할 수없는 것은 파일 이름이 이름이고 전체 주소가 아님을 나타냅니다.PHP 헤더 강제 다운로드 - 작동하지 않습니다
건배, 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;
?>
Tks! Vikk, 이제 저장하고 있지만 이미지를 열려고하면 파일이 열리지 않습니다. 미리보기에서 인식 할 수없는 파일 형식이나 파일이 손상되었을 수 있습니다. – Mango
하나는 헤더에있는 것이 이미지의 이름인지 이해할 수 없다고 생각합니다. 올바른 폴더를 말하지 않고 어떻게 작동합니까? – Mango
'readfile ($ filename); 대신'readfile ($ filepath);'을 실행하면 올바르게 다운로드됩니다. – Vikk