PHP
PHP 이미지 다운로드 또는 바로보기
씨엔아이소프트
2022. 2. 11. 02:52
반응형
서버에 있는 이미지를 브라우저에 바로 보여주는 방법과 다운로드 되게 하는 방법이다.
두 함수의 차이점은 Content-Disposition: attachment 와 Content-Disposition: inline 이다.
inline은 바로 보여주는 방식이고 attachment는 다운로드 방식이다.
function image_down($img_path) {
$IMAGE_PATH = $img_path;
$IMAGE_SIZE = getimagesize($IMAGE_PATH);
if($IMAGE_SIZE) {
$FILENAME = 'download.'.strtolower(substr($IMAGE_PATH,strlen($IMAGE_PATH)-3,3));
header("Content-Type: ".$IMAGE_SIZE['mime']);
header("Content-Disposition: attachment;filename=$FILENAME");
header("Content-Length: ".filesize($IMAGE_PATH));
readfile($IMAGE_PATH);
}
}
function image_view($img_path) {
$IMAGE_PATH = $img_path;
$IMAGE_SIZE = getimagesize($IMAGE_PATH);
if($IMAGE_SIZE) {
$FILENAME = 'download.'.strtolower(substr($IMAGE_PATH,strlen($IMAGE_PATH)-3,3));
header("Content-Type: ".$IMAGE_SIZE['mime']);
header("Content-Disposition: inline;filename=$FILENAME");
header("Content-Length: ".filesize($IMAGE_PATH));
readfile($IMAGE_PATH);
}
}
반응형