PHP 다운로드 소스 채택완료
PHP 를통한 파일 다운로드 소스를 짜보았습니다.
</span></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"><head></span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"><meta charset="utf-8"></span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"></head></span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">
</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"><?php</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">ob_start();</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> // 파일이 있는 디렉토리</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> $downfiledir = "upload/";</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">$downfile = str_replace(" ","+", $_GET['file'] );</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">
</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">
</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> // 파일 존재 유/무 체크</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> if ( file_exists($downfiledir.$downfile ) ) {</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">
</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> header("Content-Type: application/octet-stream");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> Header("Content-Disposition: attachment;; filename=$downfile ");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> header("Content-Transfer-Encoding: binary");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> Header("Content-Length: ".(string)(filesize($downfiledir.$downfile )));</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> Header("Cache-Control: cache, must-revalidate");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> header("Pragma: no-cache");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> header("Expires: 0");</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> $fp = fopen($downfiledir.$downfile , "rb"); //rb 읽기전용 바이러니 타입</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> while ( !feof($fp) ) {</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> echo fread($fp, 100*1024); //echo는 전송을 뜻함.</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> }</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> fclose ($fp);</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> flush(); //출력 버퍼비우기 함수..</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> }</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> else {</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> ?><script>alert("존재하지 않는 파일입니다.");history.back()</script><?</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;"> }</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">
</span></font></p><p><font color="#333333" face="Helvetica Neue, Helvetica, Arial, Apple SD Gothic Neo, Malgun Gothic, Dotdum"><span style="font-size: 14px; line-height: 20px;">?></span></font></p><p><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, 'Apple SD Gothic Neo', 'Malgun Gothic', Dotdum; font-size: 14px; line-height: 20px; background-color: rgb(255, 255, 255);">
뭐 남이 짠거 좀 복사해오기는 했지만..
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"); |
가 헤더 입니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인