2010-10-12 107 views
0

我想用下面的代碼掩蓋我的下載鏈接。php標題問題

它是差不多工作 - 它似乎得到正確的文件,但是當它下載文件只有4kB的大小。

任何人都可以提供任何建議嗎?

謝謝!

也請讓我知道如果你需要任何額外的細節 - 我使用FF3.5.13

<?php 

    $filename="download.zip"; 

    $folder = 'downloads'; 

    $abs_path = $_SERVER['DOCUMENT_ROOT']; 
    $path = $abs_path . "/" . $folder ."/" .$filename; // the location of the file. 
    $mm_type="application/zip"; //this is for .zip files - Change this for other file types. 

    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer"); 
    header("Content-Type: " . $mm_type); 
    header('Content-Disposition: attachment; filename="'.basename($path).'"'); 
    header("Content-Length: " . filesize($path)); // **code edited as per comments below** 
    header("Content-Transfer-Encoding: binary"); 

    readfile($path); //Output file for download. 

    exit(); 

?> 

UPDATE上MAMP運行以下命令:這裏是什麼是裏面的文件生成


警告:filesize()[function.filesize]:stat在/ 0123應用程序/MAT/htdocs/downloads/download.zip中出現故障/Applications/MAMP/htdocs/CURRENT/test.php在線

警告:不能更改頭信息 - 頭已經發出在/應用程序/ MAMP/htdocs中/電流/測試(輸出開始/Applications/MAMP/htdocs/CURRENT/test.php:15)。在線PHP

警告:不能更改頭信息 - 頭已經發出的(輸出開始/Applications/MAMP/htdocs/CURRENT/test.php:15)/應用程序/ MAMP/htdocs/CURRENT/test.php on line

警告:ReadFile的(/Applications/MAMP/htdocs/downloads/download.zip)[function.readfile]:未能打開流:在/應用程序/ MAMP/htdocs中/電流沒有這樣的文件或目錄/ test.php的在線

+2

該文件包含什麼?我敢打賭這是一個PHP錯誤信息 – 2010-10-12 10:48:07

+0

嘗試在文本編輯器中打開文件。 – 2010-10-12 10:53:32

+0

查看更新後 - – Simon 2010-10-12 11:07:50

回答

1
header("Content-Length: " . filesize($file)); 

凡$文件被設置?

+0

根據上面的帖子更正 - 仍然不工作 - 輸出文件仍然是4kB。 – Simon 2010-10-12 10:52:58

3

我認爲這是在哪兒買錯了:

header("Content-Length: " . filesize($file)); 

將其更改爲:

header("Content-Length: " . filesize($path)); 
+0

做了這個改變 - 但下載的文件大小似乎仍然只有4kB – Simon 2010-10-12 10:52:13

0

嘗試:

header ("Content-Disposition: attachment; filename=".$name."\n\n"); 

header ("Content-Type: application/octet-stream"); 

header ("Content-Length: ".filesize($complete_path_file)); 

readfile($complete_path_file); 

或者還改變內容類型頭( 「內容類型:應用程序/強制下載」);

1

此消息

警告:文件大小()[function.filesize]:STAT /應用程序/ MAMP/htdocs中/電流/試驗失敗/Applications/MAMP/htdocs/downloads/download.zip 。第15行

PHP是非常明顯的:您的文件不存在。