-
[PHP] 파일의 절대위치 감출 때 유용한 readfile 함수PHP 2024. 1. 11. 21:11
readfile
readfile
함수는 PHP에서 파일을 읽어서 클라이언트에게 출력하는데 사용됩니다.
파일을 읽고 출력하는 데 효과적입니다.
기본 구문
readfile(filename, include_path, context);
예제
우선 실습하기 위해 a.txt 파일을 만들어 작성한다
//a.txt Hello PHP !!
다음으로 다운로드 링크로 이동 할 downloadLink.php 를 생성 후 작성합니다.
- downloadLink.php <a href="readfile.php">파일 다운로드</a>
그 후 실제 작동 할 readfile.php 파일을 생성 후 작성합니다
<?php $filepath = 'a.txt'; $filesize = filesize($filepath); // 파일 사이즈 구하기 $filename = 'b.txt'; $is_member = true; if ($is_member === true) { // 헤더 설정 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); } else { echo "회원만 다운로드 가능합니다."; } ?>
이렇게 한 후 해당 URL 로 접근하면 자동으로 파일이 내 컴퓨터에 저장되는 것을 확인 할 수 있습니다.
'PHP' 카테고리의 다른 글
[PHP] 파일 확장자 찾는 함수 만들기 (0) 2024.02.21 [PHP] 파일 읽기, 쓰기 , 열기 , 닫기 (0) 2024.02.21 [PHP] 파일 업로드 (1) 2024.01.11 [PHP] include / require (1) 2024.01.11 [PHP] date (0) 2024.01.11