I have had several problems trying adding files, because of a path problem. The error gived was this:
ZipArchive::addFile() [function.ZipArchive-addFile]: Unable to access <path>
I used an absolute root starting by "/", and it didn't work. Try starting your path with "./" (referencing the root of your site).
ZipArchive::addFile
(No version information available, might be only in CVS)
ZipArchive::addFile — 指定したパスからファイルを ZIP アーカイブに追加する
説明
bool ZipArchive::addFile
( string $filename
[, string $localname
] )
指定したパスから、ファイルを ZIP アーカイブに追加します。
パラメータ
- filename
-
追加するファイルへのパス。
- localname
-
ZIP アーカイブ内部での名前。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
この例は、ZIP ファイルアーカイブ test.zip をオープンし、ファイル /path/to/index.txt を newname.txt という名前で追加します。
例1 オープンおよび抽出
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo '成功';
} else {
echo '失敗';
}
?>
ZipArchive::addFile
javierseixas at gmail dt com
17-Jul-2008 05:30
17-Jul-2008 05:30
wacher at freemail dot hu
23-Jun-2008 10:03
23-Jun-2008 10:03
The workaround above (file_get_contents) is very dangerous if you pack large files. (see memory limit).
Close/open the zip archive periodically instead of using file_get_contents().
stanleyshilov {} gmail.com
11-May-2008 09:05
11-May-2008 09:05
It should be noted that the example provided above is not accurate.
Unlike extractTo, zip_open does not return a boolean result, so the above example will always fail.
mike at thetroubleshooters dot dk
07-Feb-2008 04:20
07-Feb-2008 04:20
What is worse is that when you run out of filedescriptors it seems to fail silently, I have not been able to find any errors in any logfiles.
Andreas R. newsgroups2005 at geekmail de
04-Apr-2007 03:29
04-Apr-2007 03:29
Currently the number of files that can be added using addFile to the ZIP archive (until it is closed) is limited by file descriptors limit. This is an easy workaround (on the bug links below you can find another workarounds):
<?php
/** work around file descriptor number limitation (to avoid failure
* upon adding more than typically 253 or 1024 files to ZIP) */
function addFileToZip( $zip, $path, $zipEntryName ) {
// this would fail with status ZIPARCHIVE::ER_OPEN
// after certain number of files is added since
// ZipArchive internally stores the file descriptors of all the
// added files and only on close writes the contents to the ZIP file
// see: http://bugs.php.net/bug.php?id=40494
// and: http://pecl.php.net/bugs/bug.php?id=9443
// return $zip->addFile( $path, $zipEntryName );
$contents = file_get_contents( $path );
if ( $contents === false ) {
return false;
}
return $zip->addFromString( $zipEntryName, $contents );
}
?>
