✔️ IE 8, 크롬 28에서 테스트하였습니다.
- PHP Force Download
- PHP에서 파일 다운로드 구현
1 소스 코드[ | ]
PHP
Copy
<?php
$filepath = './hello_world.txt';
$filesize = filesize($filepath);
$path_parts = pathinfo($filepath);
$filename = $path_parts['basename'];
$extension = $path_parts['extension'];
header("Pragma: public");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $filesize");
ob_clean();
flush();
readfile($filepath);
- 예제: http://example.zetawiki.com/php/download1.php
- 원래는 이 코드로 한글 파일명도 잘 되어야 하는데, pathinfo가 멀티바이트를 지원하지 않아서 제대로 안된다.[1]
ob_clean();
,flush();
는 docx 다운로드 문제 해결을 위해 추가함
2 같이 보기[ | ]
3 주석[ | ]
- ↑ PHP 5.3 기준. 상위버전에서는 해결될지도...
4 참고[ | ]
편집자 Jmnote 211.35.250.69 Jmnote bot 39.7.54.77
로그인하시면 댓글을 쓸 수 있습니다.