编辑:这个问题在下面回答。如果你想ZIP的目录/文件夹就像我所做的,看到这个:How to zip a whole folder using PHP文件添加到现有的ZIP文件
我有一个应用程序自动从我的服务器下载一个ZIP文件的计时器。
但ZIP文件每天都被改变了。
当有人使用的应用程序,因为去除ZIP文件并重新添加(因为应用程序定时器每900毫秒执行的是)的应用程序的用户将得到一个“550文件不可用”错误。
所以不是删除ZIP文件,并用新的数据重新创造它,如何添加,而无需重新创建ZIP文件中的新数据?
目前我使用这个:
$zip = new ZipArchive;
// Get real path for our folder
$rootPath = realpath('../files_to_be_in_zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('../zouch.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
而这个代码得到files_to_be_in_zip
文件夹的内容,并重新使用它创建了“zouch.zip”文件。
是的,我知道新的数据FULLPATH ......这是$recentlyCreatedFile
编辑:我发现这个代码在http://php.net/manual/en/ziparchive.addfile.php
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
但我想创建一个目录现有的ZIP也是如此。
任何帮助吗?
谢谢!
卸下'ZipArchive :: CREATE | ZipArchive :: OVERWRITE'参数修复了一切!非常感谢:-) – MatrixCow08
高兴地帮助,感谢您及时接受答案。 – coderodour