파일다운

PHP 2013. 10. 24. 17:01

<?
 include $_SERVER["DOCUMENT_ROOT"]."/include/lib.php";
?>
<?
// 9. 파일 이름과 실제 파일
$dir = $_SERVER["DOCUMENT_ROOT"]."/images";
$file_path = $dir."/a.jpg";
$original = "a.jpg";
$HTTP_USER_AGENT = $_SERVER[HTTP_USER_AGENT];

// 10.파일이 있으면
if (file_exists($file_path)) {

    // 11. 다운로드 헤더 만들기

    if(eregi("(MSIE 5.0|MSIE 5.1|MSIE 5.5|MSIE 6.0)", $HTTP_USER_AGENT))
    {
      if(strstr($HTTP_USER_AGENT, "MSIE 5.5"))
      {
        header("Content-Type: doesn/matter");
        header("Content-disposition: filename=$original");
        header("Content-Transfer-Encoding: binary");
        header("Pragma: no-cache");
        header("Expires: 0");
      }

      if(strstr($HTTP_USER_AGENT, "MSIE 5.0"))
      {
        Header("Content-type: file/unknown");
        header("Content-Disposition: attachment; filename=$original");
        Header("Content-Description: PHP3 Generated Data");
        header("Pragma: no-cache");
        header("Expires: 0");
      }

      if(strstr($HTTP_USER_AGENT, "MSIE 5.1"))
      {
        Header("Content-type: file/unknown");
        header("Content-Disposition: attachment; filename=$original");
        Header("Content-Description: PHP3 Generated Data");
        header("Pragma: no-cache");
        header("Expires: 0");
      }
    
      if(strstr($HTTP_USER_AGENT, "MSIE 6.0"))
      {
        Header("Content-type: application/x-msdownload");
        Header("Content-Length: ".(string)(filesize("$file_path")));
        Header("Content-Disposition: attachment; filename=$original");
        Header("Content-Transfer-Encoding: binary");
        Header("Pragma: no-cache");
        Header("Expires: 0");
       
      }


    } else {

      Header("Content-type: doesn/matter");  
      Header("Content-Length: ".(string)(filesize("$file_path")));
      Header("Content-Disposition: attachment; filename=$original");
      Header("Content-Description: PHP3 Generated Data");
      Header("Pragma: no-cache");
      Header("Expires: 0");

    }
    flush();

    // 12. 파일을 읽어 내보내기
    if (is_file($file_path)) {
        $fp = fopen($file_path, "rb");

        while(!feof($fp)) {
            echo fread($fp, 100*1024);
            flush();
        }
        fclose ($fp);
        flush();
    }
   

} else {
    echo $alert_header;
    alert("파일이 존재 하지 않습니다.");
}
?>
[출처: PHPer's Heaven  http://handsome.pe.kr/

'PHP' 카테고리의 다른 글

Class 사용법  (0) 2013.10.27
파일업로드 시 파일명 관리  (0) 2013.10.24
PHP 환경변수  (0) 2013.10.24
쿠키  (0) 2013.10.24
조건 반복문  (0) 2013.10.24
Posted by 초보용
,