使用 zipArchive addFile() 不会将图像添加到 zip
我已经从事这个工作有一段时间了。这实际上曾经有效过一次,然后就再也没有成功过。它根本不创建 zip 文件。该文件确实存在。
$zip = new ZipArchive();
$filename = "./test" . time() .".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
如果我添加类似的内容
$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
,它会创建包含 txt 文件的 zip 文件。但不能使用任何类型的现有文件。
I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist.
$zip = new ZipArchive();
$filename = "./test" . time() .".zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
If I add something like
$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");
it creates the zip with the txt file in it.. but a no go for anytype of existing file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ZipArchive::addFile() 方法接受文件路径作为其第一个参数,但并非所有路径都是相同的。 addFile() 方法会默默地拒绝该文件,您永远不知道出了什么问题。从问题中可以得出,另一种方法是:
除了使代码正常工作之外, file_get_contents() 还会生成不错的错误消息。
The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:
In addition to getting the code working, file_get_contents() also generates decent error messages.
ZipArchive::addFile()
方法接受文件路径作为其第一个参数。Here, you are using :
这意味着您正在尝试将
/trash-icon.png
文件添加到您的存档中。Are you sure this file exists ?
请注意该文件路径的开头有一个
/
,这表明它是绝对路径。也许应该删除
/
以使用相对路径?The
ZipArchive::addFile()
method accepts the path to the file as its first parameter.Here, you are using :
Which means you are trying to add the
/trash-icon.png
file to your archive.Are you sure this file exists ?
Note there is a
/
at the beginning of that file's path, which indicates it's an absolute path.Maybe that
/
should be removed, to use a relative path ?我遇到了类似的问题,它与我要添加到 zip 存档中的文件有关。
在将文件添加到 zip 存档之前,最好检查该文件是否存在。
I had similar kind of issue and it was related with the file that I was going to add to the zip archive.
Before adding file to zip archive, it's always better to check if the file exists.