我有一個腳本從服務器上下載文件,所有的作品都很好。但是,當文件被下載時,它是一個JPG或PNG文件。我似乎無法用任何程序打開它來查看它。 (Windows照片庫或煙花)使用fopen(),fread(),feof()下載文件...下載的文件被破壞
當我下載一個視頻文件(AVI)。我無法用windows媒體播放器打開它,但我可以用vlc媒體播放器打開它。
我總是收到錯誤消息,不能讀取文件或文件損壞。
這裏是代碼,它是可靠的,或者我應該考慮使用fsocketopen或curl也許。
我試過改變標題尋找更多的答案在網絡上,沒有運氣。
任何人有一個想法是什麼錯?
chmod("extensions_img/test.jpg",0755);
$fullPath = "http://www.website_url.com/extensions_img/test.jpg";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize("extensions_img/test.jpg");
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch($ext) {
case "pdf":
$ctype = "application/pdf";
break;
case "exe":
$ctype = "application/octet-stream";
break;
case "zip":
$ctype = "application/zip";
break;
case "doc":
$ctype = "application/msword";
break;
case "xls":
$ctype = "application/vnd.ms-excel";
break;
case "ppt":
$ctype = "application/vnd.ms-powerpoint";
break;
case "gif":
$ctype = "image/gif";
break;
case "png":
$ctype = "image/png";
break;
case "jpeg":
$ctype = "image/jpg";
break;
case "jpg":
$ctype = "image/jpg";
break;
case "mp3":
$ctype = "audio/mp3";
break;
case "wav":
$ctype = "audio/x-wav";
break;
case "wma":
$ctype = "audio/x-wav";
break;
case "mpeg":
$ctype = "video/mpeg";
break;
case "mpg":
$ctype = "video/mpeg";
break;
case "mpe":
$ctype = "video/mpeg";
break;
case "mov":
$ctype = "video/quicktime";
break;
case "avi":
$ctype = "video/x-msvideo";
break;
case "src":
$ctype = "plain/text";
break;
default:
$ctype = "application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: " . $ctype);
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-Transfer-Encoding: binary");
header("Content-length: $fsize");
header("Cache-control: public"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 4096);
flush();
}
}
fclose ($fd);
試着用文本文件做它,然後看文件的內容。這可能是因爲有PHP輸出到緩衝區的錯誤消息,所以他們在垃圾文件。 – prodigitalson
'$ fsize'總是'0',因爲文件不存在。 – KingCrunch