2018년 5월 22일 화요일

[PHP예제] 231 강제로 파일 다운로드



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$fileName = 'test.jpg';
$fileSize = filesize($fileName);
$mime = 'image/jpeg';
//$mime = 'application/octet-stream'; // MIME 타입이 불분명한 경우

header('Content-Type: ' . $mime);
// IE가 Content-Type 헤더를 무시하지 않게 함
header('X-Content-Type-Optoins: nosniff');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: no-store, no-cache, must-revalidate');

if( isset($_SERVER['HTTP_USER_AGENT']) &&
    strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
{
  header('Cache-Control: post-check=0, pre-check=0', false);
  header('pragma: public');
}
header('Content-Length: ' . $fileSize);

readfile($fileName);

test.jpg 파일이 다운로드 됨

댓글 없음:

댓글 쓰기