ZIP文件下载php读取文件()错误
我知道这个问题在这个论坛发布了很多次,但相信我我已经尝试了所有可能的解决方案建议,但它并没有为我工作。ZIP文件下载php读取文件()错误
我试图下载使用压缩多个文件,但zip文件被下载的成功,它的腐败,我在记事本打开它后遇到错误:
警告:ReadFile的(E:\下载/ IMG -20140831-WA0000.zip)[function.readfile]:未能打开流:在...
我尝试了所有在论坛中提到的可能的解决方案,如头检查没有这样的文件或目录,网页服务器用户对我创建ZIP文件的文件夹具有写入权限,下载前进行错误检查等,但它没有任何问题d出来。
进行错误检查后,我遇到了类似
错误创建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") . '/';
的目录名称功能 “Returns parent directory's path”。这意味着$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\\";
@SevrriMOslen根据你的建议和来自php手册的教程,我改变了'dirname(“E:\ Downloads”)。 '/';'到'dirname(__ FILE__);'nd'$ file_path = dirname($ archive_file_name)。 '/';'最后'$ fileNames = array(\ folder1 /'。$ fullName1,'\ folder2 /'。$ fullName2);'。该代码终于工作了,Zip文件成功下载没有任何损坏的消息。感谢您的支持。 – 2015-04-05 15:18:54
'目录名( “E:\下载”)''返回 “E:\”'。如果你不使用'dirname()'会发生什么? – 2015-04-05 07:37:02
另外,'$ actualName'从哪里来? – 2015-04-05 07:42:32
@SevrriMOslen'$ actualName'是要下载的zipFile的名称。例如。如果'$ actualName ='hello';'然后'$ zip_file_name = hello.zip' – 2015-04-05 15:09:50