【发布时间】:2019-11-22 22:49:15
【问题描述】:
我目前有这个目录结构:
upload-here
upload-here/258/Image1.jpg
upload-here/259/Image2.jpg
upload-here/260/NO IMAGE
upload-here/261/somepicturename.jpg
等等等等。我正在使用下面显示的代码通过压缩来备份 upload-here 文件夹。
<?php
// Get real path for our folder
$rootPath = realpath('/full-path-to/upload-here/');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('upload-here.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();
?>
这仅适用于一个问题:如果文件夹中没有图像,则不会备份该文件夹。
我怎样才能调整代码,以便它也压缩空文件夹(因此确切的结构与在线内容相匹配)?
==================== 添加了
echo "<Br>". $filePath . "<br>". $relativePath . "<br>". $name ;
在最后两个 } 之前。这给出了:
/home/fullpath/public_html/upload-here
/home/fullpath/public_html/upload-here/.
/home/fullpath/public_html
/home/fullpath/public_html/upload-here/..
/home/fullpath/public_html/upload-here/73
73
/home/fullpath/public_html/upload-here/73/.
/home/fullpath/public_html/upload-here
/home/fullpath/public_html/upload-here/73/..
/home/fullpath/public_html/upload-here/283
283
/home/fullpath/public_html/upload-here/283/.
/home/fullpath/public_html/upload-here
/home/fullpath/public_html/upload-here/283/..
/home/fullpath/public_html/upload-here/284
284
/home/fullpath/public_html/upload-here/284/.
/home/fullpath/public_html/upload-here
/home/fullpath/public_html/upload-here/284/..
/home/fullpath/public_html/upload-here/291
291
/home/fullpath/public_html/upload-here/291/.
/home/fullpath/public_html/upload-here
/home/fullpath/public_html/upload-here/291/..
/home/fullpath/public_html/upload-here/292
292
/home/fullpath/public_html/upload-here/292/.
/home/fullpath/public_html/upload-here
等等等等
【问题讨论】:
标签: php zip ziparchive