내 머릿속 데이터베이스

동영상파일 스트리밍 실행이 아닌 다운로드가 되도록 하기 본문

Programming/Web Language

동영상파일 스트리밍 실행이 아닌 다운로드가 되도록 하기

파도소리 2007. 9. 1. 19:41

웹서버가 브라우저에 파일을 보낼 때 해당 파일의 형식도 헤더로 함께 보내주게 되는데,
만일 그 형식이 동영상이면 스트리밍이나 인터넷 임시폴더에 저장되어 보여주게 된다.

그래서 일반적인 실행 말고 일반적인 파일을 다운 받듯이 다운받게 하려면,
PHP의 header() 함수를 이용하면 되는데,
그 파일을 그대로 링크하면 안되고 다운받는 PHP 파일을 만들어서 header() 함수로 헤더정보를 바꿔준 다음에 파일을 읽어서 보내면 된다.

예) download.php

header("Content-Disposition: attachment; filename=filename.asf");
header("Content-Transfer-Encoding: binary");
header("Content-Type: application/octec-stream");

fp = fopen("파일명");
//파일을 읽어서 그대로 echo 로 출력하는 루틴
 fclose(fp);

사용을 하려면
download.php?filename=test.asf


관련 풀 소스(모든 파일 다운로드 가능)
$FileName = "파일명";
$File = "파일이 위치한경로".$FileName;
if(is_file($file)){ //실제로 파일이 있다면 다운로드 수행
    if(eregi("(MSIE 5.5|MSIE 6.0)", $HTTP_USER_AGENT)){
        header("Content-type: application/octet-stream");
        header("Content-Length: ".filesize("$File"));
        header("Content-Disposition: attachment; filename=$FileName");
        header("Content-Transfer-Encoding: binary");
        header("Pragma: no-cache");
        header("Expires: 0);
    }
    else{
        header("Content-type: file/unknown");
        header("Content-Length: ".filesize("$File"));
        header("Content-Disposition: attachment: filename=$FileName");
        header("Content-Description: PHP3 Generated Data");
        header("Pragma: no-cache");
        header("Expires: 0");
    }
    $fp = fopen($File, "rb");
    if(fpassthru($fp)){ //다운로드가 완료되면
        //여기에서 다운로드 카운트를 올리면 됨다.(필요없으면 안해도 됨)
    }
    else{
        fclose($fp);
    }
}
else{ //파일이 없는경우
    echo "파일이 존재하지 않습니다.";
}
Comments