其实我正在创建一个项目,我必须创建一个文件夹的zip文件,但这些文件夹是动态的,每次该文件夹在服务器上都可用时没有必要。所以我需要你的帮助告诉我当服务器上没有文件夹时我如何处理代码。以下是我的代码
$is = $rowm["id"];
// Get real path for our folder
$rootPath = realpath("documents/$is");
// Initialize archive object
$zip = new ZipArchive();
//$abc = 'file';
$zip->open("$is.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();
在上面的代码" $ is"是动态发生的文件夹的名称。