Php 下载的.RAR文件打开时没有任何内容

Php 下载的.RAR文件打开时没有任何内容,php,Php,对于我自己的网站,我想有一个页面,您可以从中下载.rar文件。但是当我下载并打开文件时。我收到一个信息框,上面写着:“没有找到档案。” 我不知道该怎么办,这是我的代码 $filename="inputValidatorCasd.rar"; $file="c:\\wamp\\www\\DennisWeb\\Files\\$filename"; $len = filesize($file); ob_clean(); header("Pragma: public"); header("Expi

对于我自己的网站,我想有一个页面,您可以从中下载.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;

您可以使用以下方法下载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);

它可能会帮助您。

我没有收到错误。尝试从
@readfile($file)
中删除
@
,您可能会收到错误消息
@
会抑制错误,这是一种非常糟糕的做法。这样做了吗,没有任何更改。注释掉所有标题,然后将输出发送到浏览器,以查看它是否实际获得任何内容…这样做了,我相信没有内容,但我给出的路径是正确的。将.zip文件重命名为.rar确实会使其成为rar存档。此外,问题不在于如何创建归档。