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

发布于 2024-10-25 20:42:35 字数 674 浏览 2 评论 0原文

我已经从事这个工作有一段时间了。这实际上曾经有效过一次,然后就再也没有成功过。它根本不创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2024-11-01 20:42:35

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

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

除了使代码正常工作之外, 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:

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

跨年 2024-11-01 20:42:35

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

Here, you are using :

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

这意味着您正在尝试将 /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 :

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

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 ?

柳絮泡泡 2024-11-01 20:42:35

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

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

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

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.

$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
if (file_exists($thisdir . "/trash-icon.png")) {
    $zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文