php 使用 zipArchive addFile() 不会将图像添加到 zip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5422983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:21:24  来源:igfitidea点击:

using zipArchive addFile() will not add image to zip

phpziparchive

提问by Vance

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 文件。该文件确实存在。

$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.

它创建了包含 txt 文件的 zip 文件......但不适用于任何类型的现有文件。

回答by JohnnyB

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:

ZipArchive::addFile() 方法接受文件的路径作为其第一个参数,但并非所有路径都创建相同。addFile() 方法默默地拒绝文件,你永远不知道出了什么问题。从问题中可以得出,另一种方法是:

// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);

In addition to getting the code working, file_get_contents() also generates decent error messages.

除了使代码正常工作之外,file_get_contents() 还会生成不错的错误消息。

回答by Pascal MARTIN

The ZipArchive::addFile()method accepts the path to the file as its first parameter.

ZipArchive::addFile()方法接受文件路径作为其第一个参数。


Here, you are using :


在这里,您正在使用:

$zip->addFile("/trash-icon.png", "/gabage.png");

Which means you are trying to add the /trash-icon.pngfile to your archive.

这意味着您正在尝试将/trash-icon.png文件添加到存档中。


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 ?

请注意,该文件路径的开头有一个/,表示它是绝对路径。
也许/应该删除,以使用相对路径?

回答by Mukesh Chapagain

I had similar kind of issue and it was related with the file that I was going to add to the zip archive.

我有类似的问题,它与我要添加到 zip 存档的文件有关。

Before adding file to zip archive, it's always better to check if the file exists.

在将文件添加到 zip 存档之前,最好检查文件是否存在。

$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
if (file_exists($thisdir . "/trash-icon.png")) {
    $zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
}

回答by Skytiger

See the answer to this question - it also works for images, and uses addFile instead of addFromString, which as far as I know is lighter in memory usage.

请参阅此问题的答案 - 它也适用于图像,并使用 addFile 而不是 addFromString,据我所知,它在内存使用方面更轻。