略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ZipArchive::close

2024-11-13

ZipArchive::close

(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.1.0)

ZipArchive::closeClose the active archive (opened or newly created)

说明

public ZipArchive::close(): bool

Close opened or created archive and save changes. This method is automatically called at the end of the script.

If the archive contains no files, the file is completely removed (no empty archive is written).

参数

此函数没有参数。

返回值

成功时返回 true, 或者在失败时返回 false

add a noteadd a note

User Contributed Notes 9 notes

up
38
jared at kippage dot com
12 years ago
It may seem a little obvious to some but it was an oversight on my behalf.

If you are adding files to the zip file that you want to be deleted make sure you delete AFTER you call the close() function.

If the files added to the object aren't available at save time the zip file will not be created.
up
16
john factorial
11 years ago
If you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist. Apparently $zip->addFile() returns TRUE even if the file doesn't actually exist. It's a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.
up
12
javier dot santacruz at avature dot net
5 years ago
ZipArchive.close() changes its behaviour in PHP7. The function ignores directories in PHP5 but fails in PHP7 with:

Unexpected PHP error [ZipArchive::close(): Read error: Is a directory]

The following code works in PHP5 but not in PHP7:

<?php
// test.php
$zip = new ZipArchive();
$zip->open('/tmp/test.zip', ZipArchive::CREATE);
$zip->addFile('.', '.');
$ret = $zip->close();
echo
"Closed with: " . ($ret ? "true" : "false") . "\n";
?>

For php5:

php --version
  PHP 5.5.38-1-avature-ondrej-fork (cli) (built: Aug 31 2016 16:37:38)

php test.php
  Closed with: true

For php7:

php --version
  PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )

php test.php
  Closed with: false
up
11
m021 at springtimesoftware dot com
11 years ago
If you have created a zip file and added a file to it without error, yet the ZipArchive::close call fails (with ER_TMPOPEN: "Failure to create temporary file") and the zip file is not created, check to see if your ZipArchive::open call specifies a pathname containing nonexisting directories. If you expect a containing hierarchy of one or more directories, you must create them yourself before using using ZipArchive. You can write a simple function to recurse using dirname to find each parent directory, creating those that don't exist by using mkdir when leaving the recursion.
up
4
gilthans at gmail dot com
14 years ago
Don't forget to check the zip isn't empty, folks - otherwise the zip won't be created at all, and the server will issue no warning!

I used a certain loop to add files to the zip, and struggled with permissions and documentation for hours before I realize the loop ended up adding no file, even though addFile WAS called, but on a non-existent file.
This might be the reason your zips aren't popping up.
up
1
valera at creator dot su
7 years ago
Pay attention, that ZipArchive::addFile() only opens file descriptor and does not compress it. And only ZipArchive::close() compress file and it take quite a lot of time. Be careful with timeouts.
up
0
luciandex
6 months ago
I am not sure, but seems that $zip->close() can run into errors if the path specified in $zip->open is a relative path, not absolute.
So, when create archives, please specify ABSOLUTE PATH, not relative path for zip file to be created.
up
-6
webnull dot www at gmail dot com
11 years ago
Please note you must have write permissions to file and directory where the files is placed to save the archive.
up
-15
Enyby
5 years ago
If you open archive without flags in PHP 5.6 then you can get errors on close:
Warning: ZipArchive::close(): Zip archive inconsistent in
And changes not saved.
You need use flag ZIPARCHIVE::OVERWRITE on $zip->open.

This issue happens for me only in php 5.6. In php 5.4 and before it works without any flags to $zip->open.

官方地址:https://www.php.net/manual/en/ziparchive.close.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3