close

一開始可以下載檔案

就沒有太注意下載後的檔案狀況

 

點擊檔案後出現"檔案似乎屬於不支援格式 無法開啟"

 

原來是檔案前面有奇怪的字元

加上ob_clean()

檔案才是正確的

 

if(isset($_GET['attachment_name']) && $_GET['attachment_name'] != ""){
    $file = $gs_upload_folder . "/" . $_GET['attachment_name'];

    if (file_exists($file)) {
        switch(strtolower(substr(strrchr($_GET['attachment_name'],'.'),1)))
        {
            case 'pdf': $mime = 'application/pdf'; break;
            case 'zip': $mime = 'application/zip'; break;
            case 'jpeg':
            case 'jpg': $mime = 'image/jpg'; break;
            case 'png': $mime = 'image/png'; break;
            default: $mime = 'application/force-download';
        }
        header('Content-Description: File Transfer');
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false);
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        header('Last-Modified: ' . gmdate('D, d M Y H:i ') . ' GMT');
        header('Content-Transfer-Encoding: binary');
        ob_clean();
        flush();
        readfile($file);
        exit;
    } else {
        echo "檔案不存在";
    }
}

arrow
arrow

    小小朋 發表在 痞客邦 留言(0) 人氣()