PHP 다운로드 소스
본문
PHP 를통한 파일 다운로드 소스를 짜보았습니다.
<head>
<meta charset="utf-8">
</head>
<?php
ob_start();
// 파일이 있는 디렉토리
$downfiledir = "upload/";
$downfile = str_replace(" ","+", $_GET['file'] );
// 파일 존재 유/무 체크
if ( file_exists($downfiledir.$downfile ) ) {
header("Content-Type: application/octet-stream");
Header("Content-Disposition: attachment;; filename=$downfile ");
header("Content-Transfer-Encoding: binary");
Header("Content-Length: ".(string)(filesize($downfiledir.$downfile )));
Header("Cache-Control: cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen($downfiledir.$downfile , "rb"); //rb 읽기전용 바이러니 타입
while ( !feof($fp) ) {
echo fread($fp, 100*1024); //echo는 전송을 뜻함.
}
fclose ($fp);
flush(); //출력 버퍼비우기 함수..
}
else {
?><script>alert("존재하지 않는 파일입니다.");history.back()</script><?
}
?>
뭐 남이 짠거 좀 복사해오기는 했지만..
PHP 5.X.X 버전에서는 잘 작동합니다만
제가 사용중인 서버는 싸구려라서 4.4.8 버전이 설치되어 있습니다.
당근 안될수밖에 없죠...
오류코드를 보니까 해더 변경하는 부분에서 오류가 난다고 뜨네요.
해더부분을 변경해야하는데 이제겨우 1년된 초보라서
이게 뭔말인지 알 수 없습니다. ㅠㅠ
도와주세요 !!
답변 1
정확한 에러가 없어서 어떤에러인지 모르겠네요
header( "Content-Type: application/octet-stream" ); |
20 | Header( "Content-Disposition: attachment;; filename=$downfile " ); |
21 | header( "Content-Transfer-Encoding: binary" ); |
22 | Header( "Content-Length: " .(string)( filesize ( $downfiledir . $downfile ))); |
23 | Header( "Cache-Control: cache, must-revalidate" ); |
24 | header( "Pragma: no-cache" ); |
25 | header( "Expires: 0" ); |
가 헤더 입니다.