我有這個代碼的圖像下載在PHP ...工作正常,圖像下載,但問題是,它不會打開的地方,它被下載,並給出錯誤「無法讀取文件頭...未知文件格式!「未知的文件格式在圖像下載在php
<?php
$path = $row['img_url'].".jpg";
echo $path;
$filename = $path;
$ctype="application/.jpg";
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
// change, added quotes to allow spaces in filenames
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>
如果您在任何十六進制/文本編輯器中打開下載的文件,您會看到什麼? – zerkms
那個'echo',第3行?在使用'header()'之前,你*不能輸出到緩衝區*(除非你用輸出緩衝來捕獲它)。沒有空白,沒有「回聲」,沒有「印刷」,什麼也沒有。你附加的頭文件必須在你正在構造的數據包的頭部(這是輸出到緩衝區的「標題消息內容之後」),所以它們必須在開始構建響應主體之前進行設置。 –