PHP 파일 다운로드 소스 코드
<?php
function mb_basename($path) { return end(explode('/',$path)); }
function utf2euc($str) { return iconv("UTF-8","cp949//IGNORE", $str); }
function is_ie() { return isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false; }
$filename = trim($_GET['filename']);
if(strstr($filename, "..")) {
echo "Sorry Parent Permission Denied";
exit;
}
//다운로드 이동 취약점 처리 FILE_ROOT_DIR DOWN_ROOT_DIR
if ($filename == "") {
echo "<script>alert('파일이 없습니다.');history.back(0);return false;</script>";
exit;
}
$filepath = $filename;
$filesize = filesize($filepath);
$filename = mb_basename($filepath);
if( is_ie() ) $filename = utf2euc($filename);
header("Pragma: public");
header("Expires: 0");
header("Content-Type: application/zip; charset=utf-8");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $filesize");
ob_clean();
flush();
readfile($filepath);
?>