2016-01-08 43 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档案。另外,问题不在于如何创建档案。 –