ZIP文件下载php readfile()错误


ZIP file download php readfile() error

我知道这个问题在这个论坛上发布了很多次,但相信我,我已经尝试了所有可能的解决方案,但对我来说都不起作用。

我正试图使用zip下载多个文件,尽管zip文件下载成功,但它已损坏,我在记事本中打开后遇到错误:

警告:readfile(E:''Downloads/IMG-20140831-WA0000.zip)[function.redfile]:无法打开流:在…中没有这样的文件或目录

我尝试了论坛中提到的所有可能的解决方案,如标题检查、web服务器用户对我创建ZIP文件的文件夹有写入权限、下载前的错误检查等,但都没有成功。

在进行错误检查后,我遇到了类似的情况

创建ZIP文件时出错:IMG-20140831-WA0000.ZIP

我的代码片段:

function zipFilesDownload($file_names, $archive_file_name, $file_path) {
    $zip = new ZipArchive;
    if ($zip->open($archive_file_name, ZipArchive::CREATE) !== TRUE) {
        exit("cannot open <$archive_file_name>'n");
    }
    foreach($file_names as $files) {
        $zip->addFile($file_path . $files, $files);
    }
    if ($zip->close() === false) {
        exit("Error creating ZIP file : " . $archive_file_name);
    }
    if (file_exists($archive_file_name)) {
        header("Content-Description: File Transfer");
        header("Content-type: application/zip"); 
        header("Content-Disposition: attachment; filename=" . $archive_file_name . "");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile("E:'Downloads/" . $archive_file_name);
        ob_clean();
        flush();
        exit;
    } else {
        exit("Could not find Zip file to download");
    }
}
$fileNames = array(
    'D:''xampp'htdocs'BE'Multimedia/' . $fullName,
    'D:''xampp'htdocs'BE'Decrypt/' . $decrypt_file
);
$zip_file_name = $actualName . '.zip';
$file_path = dirname("E:'Downloads") . '/';
zipFilesDownload($fileNames, $zip_file_name, $file_path);

请提出一些解决方案。

问题看起来是这一行:

$file_path = dirname("E:'Downloads") . '/';

dirname函数"返回父目录的路径"。这意味着$file_path将是E:'

在您的函数中,您使用$zip->addFile()方法中的$file_path来引用应添加到ZIP归档中的文件。

换句话说,如果你有一个文件数组,比如:

$files = array(
    'file1.txt',
    'file2.txt',
    'file3.txt',
);

然后将添加到档案的文件将是:

E:'file1.txt
E:'file2.txt
E:'file3.txt

你可能想要的是添加这些文件:

E:'Downloads'file1.txt
E:'Downloads'file2.txt
E:'Downloads'file3.txt

据我所见,要修复代码,只需而不是使用dirname(),如下所示:

$file_path = "E:'Downloads''";