2016-01-08 41 views
-1

對於我自己的網站我想要一個頁面,您可以從這裏下載.rar文件。但是,當我下載並打開文件。我收到一個消息框說:「找不到檔案。」已下載.RAR文件沒有任何內容。

我不知道該怎麼辦,這是我的代碼。

$filename="inputValidatorCasd.rar"; 
$file="c:\\wamp\\www\\DennisWeb\\Files\\$filename"; 
$len = filesize($file); 
ob_clean(); 
header("Pragma: public"); 
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:application/octet-stream"); 
$header="Content-Disposition: attachment; filename=$filename;"; 
header($header); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".$len); 
@readfile($file); 
exit; 
+0

我沒有收到錯誤。 –

+3

嘗試從'@readfile($ file)'中刪除'@',您可能會收到錯誤消息。 '@'可以消除錯誤,這是一個非常糟糕的做法。 –

+0

沒有,沒有改變。 –

回答

-2

您可以使用下面的方法來下載的RAR文件: -

$album_name = "testing" 
$files = array("1.txt", "2.txt"); // array of your files 

$zip = new ZipArchive(); 

# create a temp file & open it 
$tmp_file = tempnam('.',''); 
$zip->open($tmp_file, ZipArchive::CREATE); 

# loop through each file 
foreach($files as $file){ 

    # download file 
    $download_file = file_get_contents($file); 

    #add it to the zip 
    $zip->addFromString(basename($file),$download_file); 

} 

# close zip 
$zip->close(); 

# send the file to the browser as a download 
header('Content-disposition: attachment; filename='.$album_name.'.rar'); 
header('Content-type: application/zip'); 
readfile($tmp_file); 

它可以幫助你。

+0

將.zip文件重命名爲.rar注意使其成爲一個rar檔案。另外,問題不在於如何創建檔案。 –