Media Log

[파일다운로드]에 해당되는 글 1

  1. php 파일다운로드 소스 2008.05.15

php 파일다운로드 소스
<?
$fileName = $_REQUEST["filePath"];
$t_name = $_REQUEST["t_name"];
$file = "/data/$fileName";

// 브라우저 골라서 헤더를 따로 따로 전송 해 준다.(5.5 일때는 다르게 해줘야 함)
if(strstr($HTTP_USER_AGENT, "MSIE 6.") or strstr($HTTP_USER_AGENT, "MSIE 5.5"))
{
 Header("Content-type: application/x-msdownload");
 header("Content-type: application/octet-stream");
 header("Cache-Control: private, must-revalidate");
 header("Pragma: no-cache");
 header("Expires: 0");
}
else
{
 header("Cache-control: private");
 header("Content-type: file/unknown");
 header('Content-Length: '.filesize("$file"));
 Header("Content-type: file/unknown");
 Header("Content-Disposition: attachment; filename=$fileName");
 Header("Content-Description: PHP5 Generated Data");
 header("Pragma: no-cache");
 header("Expires: 0");
}

if(file_exists("$file"))
{
 $fp = fopen("$file", "r");
 if(!fpassthru($fp))
 fclose($fp);
}
else
{
 echo "<script>alert('해당 파일이나 경로가 존재하지 않습니다.');history.back();</script>";
}
?>