Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 排除目录&;来自RecursiveDirectoryIterator的文件_Php_Arrays_Zip_Recursive Query - Fatal编程技术网

Php 排除目录&;来自RecursiveDirectoryIterator的文件

Php 排除目录&;来自RecursiveDirectoryIterator的文件,php,arrays,zip,recursive-query,Php,Arrays,Zip,Recursive Query,我正在尝试压缩一个文件夹及其所有文件和子文件夹,除了一些文件和文件夹。 我有一系列要排除的文件和文件夹。 适用于文件,但不排除排除数组中的文件夹。。。 代码如下: if(isset($_POST['tourpath'])){ $source = $_POST['tourpath']; $cache_name = $_POST['cache_name']; $destination = $cache_name.'.zip'; if (!extension_loaded('zip') || !fi

我正在尝试压缩一个文件夹及其所有文件和子文件夹,除了一些文件和文件夹。 我有一系列要排除的文件和文件夹。 适用于文件,但不排除排除数组中的文件夹。。。 代码如下:

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();
}

如何也排除文件夹?
很多thx

检查路径中是否存在数组中的目录名的函数非常有用:

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>';
  }
}
$files=新的递归迭代器(
新的RecursiveDirectoryIterator($source,filesystemerator::SKIP_DOTS),
递归迭代器::SELF_优先
);
$excludeDir=['dir1','dir2'];
foreach($path=>$file形式的文件){
if(!$file->isDir()和!isdiripath($excludeDir,$path)){
echo$file->getFileName(),“
”; } }

注意:整个路径不必在$excludeDir数组中“dir1”足以隐藏“/foo/dir1/bar/”下的所有内容。

函数用于检查路径中是否存在数组中的目录名:

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>';
  }
}
$files=新的递归迭代器(
新的RecursiveDirectoryIterator($source,filesystemerator::SKIP_DOTS),
递归迭代器::SELF_优先
);
$excludeDir=['dir1','dir2'];
foreach($path=>$file形式的文件){
if(!$file->isDir()和!isdiripath($excludeDir,$path)){
echo$file->getFileName(),“
”; } }

注意:整个路径不必在$excludeDir数组中“dir1”足以隐藏“/foo/dir1/bar/”下的所有内容。

如果您的问题已解决,请单击答案旁边的复选标记选择最适合您的答案。如果您的问题已解决,请单击答案旁边的复选标记选择最适合您的答案。