개발

PHP 에서 파일다운로드 하기

에드몽단테스 2009. 2. 7. 12:07

파일다운로드 하는 것은 첨부파일에 링크를 거는 것으로 간단하게 할 수 있다.

하지만 첨부파일이 웹에서 접근할 수 있는 위치에 있지 않는다면 링크를 걸 수 없다.

어떻게 해야할까?

 

웹에 접근할 수 없어서 직접 링크를 걸 수는 없지만 간접적으로 접근이 가능한 파일을 만들어서 그 파일을 웹에서 호출하면 된다.

 

<?

if($status == "download")
{
    $filename = @iconv("UTF-8", "euc-kr", $info[’fname’]);
    Header("Content-Length:".filesize("upload/".$info[’fpath’]));
    Header("Content-Disposition:attachment;filename=".$filename);
    Header("Content-Transfer-Encoding:binary");
    Header("Connection:close");
    Header("Content-type: application/octet-stream;");

    $fd=fopen("upload/".$info[’fpath’], "r");
    if(!$fd)
        return exit;

    while(($str=fread($fd, 4096)))
    {
        printf("%s",$str);
    }
    fclose($fd);
    exit;
}

?>

 

파일이름이 UTF-8인경우 에는 euc-kr로 변경해준다.

반응형