繁体   English   中英

ZIP文件下载php readfile()错误

[英]ZIP file download php readfile() error

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

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

警告:readfile(E:\\ Downloads / IMG-20140831-WA0000.zip)[function.readfile]:无法打开流:...中没有此类文件或目录

我尝试了论坛中提到的所有可能的解决方案,例如标头检查,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:\\

在函数中,您使用$file_path $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\\";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM