php를 이용하여 file을 로컬저장소에 저장하기



간단한 예제 소스입니다.

<?php $file_name = 'msg.txt'; // 저장될 파일 이름 $file = './text/msg.txt'; // 파일의 전체 경로 header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $file_name . '"'); header('Content-Transfer-Encoding: binary'); header('Content-length: ' . filesize($file)); header('Expires: 0'); header("Pragma: public"); $fp = fopen($file, 'rb'); fpassthru($fp); fclose($fp); ?>


사용법은 위의 소스코드를 file_down.php 등의 파일로 만들어 두고 

버튼이나 <a> 태그로 눌렀을때 file_down.php 로 이동하게 만들면 화면은 로드되지 않고 바로 파일 다운을 실행합니다.




디렉토리는 이렇게 구성하였습니다. 'text' 폴더안에 msg.txt 파일이 존재합니다.


test.php 에는 file_down.php 를 실행하기 위한 <a>태그만 하나 만들어줍니다.



링크를 누르게 되면 file_down.php 를 실행하게 되고 자동으로 파일이 지정된 폴더로 다운로드 되게 됩니다.



저장 폴더위치는 브라우저 설정에서 변경 할 수 있으며 '크롬' 의 경우 chrome://settings/  -> 고급 -> 다운로드에서 위치를 변경하면 됩니다.



+ Recent posts