从 RecursiveDirectoryIterator 中排除目录和文件
Exclude directories & files from RecursiveDirectoryIterator
我正在尝试压缩一个文件夹及其所有文件和子文件夹,某些文件和文件夹除外。
我有一系列要排除的文件和文件夹。
适用于文件,但不排除排除数组中的文件夹...
这是代码:
if(isset($_POST['tourpath'])){
$source = $_POST['tourpath'];
$cache_name = $_POST['cache_name'];
$destination = $cache_name.'.zip';
if (!extension_loaded('zip') || !file_exists($source) || file_exists($destination)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
$exclude_files = array('zipmytour.php','krpano_sworker.js','cache_settings.js','lib','b','d','f','l','r','u');
foreach ($files as $file)
{
if (!in_array($file->getFilename(),$exclude_files)) {
$file = str_replace('\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
如何同时排除文件夹?
非常感谢!!!
一个函数很有用,它可以检查数组中的目录名称是否存在于路径中:
function isDirInPath(array $dirs, $path) {
$path = str_replace('\', '/', $path);
foreach($dirs as $dir){
if(strpos($path,'/'.trim($dir,"/").'/') !== false){
return true;
}
}
return false;
}
函数使用示例:
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source,FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
$excludeDir = ['dir1','dir2'];
foreach($files as $path => $file){
if(!$file->isDir() AND !isDirInPath($excludeDir,$path)){
echo $file->getFileName(), '<br>';
}
}
注意:整个路径不必在 $excludeDir 数组中。 'dir1' 足以隐藏“/foo/dir1/bar/”下的所有内容。
我正在尝试压缩一个文件夹及其所有文件和子文件夹,某些文件和文件夹除外。 我有一系列要排除的文件和文件夹。 适用于文件,但不排除排除数组中的文件夹... 这是代码:
if(isset($_POST['tourpath'])){
$source = $_POST['tourpath'];
$cache_name = $_POST['cache_name'];
$destination = $cache_name.'.zip';
if (!extension_loaded('zip') || !file_exists($source) || file_exists($destination)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
$exclude_files = array('zipmytour.php','krpano_sworker.js','cache_settings.js','lib','b','d','f','l','r','u');
foreach ($files as $file)
{
if (!in_array($file->getFilename(),$exclude_files)) {
$file = str_replace('\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
如何同时排除文件夹? 非常感谢!!!
一个函数很有用,它可以检查数组中的目录名称是否存在于路径中:
function isDirInPath(array $dirs, $path) {
$path = str_replace('\', '/', $path);
foreach($dirs as $dir){
if(strpos($path,'/'.trim($dir,"/").'/') !== false){
return true;
}
}
return false;
}
函数使用示例:
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source,FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
$excludeDir = ['dir1','dir2'];
foreach($files as $path => $file){
if(!$file->isDir() AND !isDirInPath($excludeDir,$path)){
echo $file->getFileName(), '<br>';
}
}
注意:整个路径不必在 $excludeDir 数组中。 'dir1' 足以隐藏“/foo/dir1/bar/”下的所有内容。