我試圖強制下載一個位於我的網站根目錄之上的目錄中的圖像。下載發生好,並保存正確的文件名。但是,最終文件不是有效的圖像,無法正常打開或顯示。這裏是我的代碼:PHP:強制從目錄根目錄下載圖像?
$photograph = new ViewPhotograph($photograph_id);
$photograph->setPhotographVars();
$file = $photograph->getPath('small');
$filename = '1.jpg';
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
header('Content-Disposition: attachment; filename=' . $filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
之前的標題? – ThinkingInBits 2010-08-19 12:22:13
Wooo ...工作。謝謝! – ThinkingInBits 2010-08-19 12:22:53
也許在文件的開頭,捕捉所有錯誤或輸出 – xXx 2010-08-19 12:29:35