PHP-在多个目录中搜索文件

PHP-在多个目录中搜索文件,php,file,search,directory,dir,Php,File,Search,Directory,Dir,我想让脚本在多个目录中搜索一个文件,例如,我有dir的结构: -images --2014 --2015 --2016 现在,我的PHP脚本应该在2014/2015/2016的其中一个目录中搜索FILE.jpg(文件夹将动态创建,因此我无法在脚本中列出文件夹列表(始终只显示文件夹“图像”) 向您致意!您只需使用glob() 在您描述的确切情况下,应如下所示: $files = glob('path_to_images/*/FILE.jpg'); function doGl

我想让脚本在多个目录中搜索一个文件,例如,我有dir的结构:

 -images
   --2014
   --2015
   --2016
现在,我的PHP脚本应该在2014/2015/2016的其中一个目录中搜索FILE.jpg(文件夹将动态创建,因此我无法在脚本中列出文件夹列表(始终只显示文件夹“图像”)


向您致意!

您只需使用
glob()

在您描述的确切情况下,应如下所示:

$files = glob('path_to_images/*/FILE.jpg');
function doGlob($target, $context) {
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images');
function doGlob($target, $context, $max_depth = -1, $depth = 0) {
  $result = glob($context . '/' . $target);
  if ($depth > $max_depth) {
    return $result;
  }
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir, $max_depth, $depth + 1));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images', <max-depth>);
一般来说,在已知文件夹路径和搜索的文件名之间根据需要放置尽可能多的
/*
,以探索给定的深度级别


根据OP的评论编辑、扩展解决方案

如果您对树结构一无所知,可以进行深入的多级搜索,如下所示:

$files = glob('path_to_images/*/FILE.jpg');
function doGlob($target, $context) {
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images');
function doGlob($target, $context, $max_depth = -1, $depth = 0) {
  $result = glob($context . '/' . $target);
  if ($depth > $max_depth) {
    return $result;
  }
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir, $max_depth, $depth + 1));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images', <max-depth>);
它将返回给定
$context
中任何位置出现的
$target
文件

注意:如果
上下文是一个大结构,可能会耗费大量时间!

因此,您可以限制搜索深度,如下所示:

$files = glob('path_to_images/*/FILE.jpg');
function doGlob($target, $context) {
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images');
function doGlob($target, $context, $max_depth = -1, $depth = 0) {
  $result = glob($context . '/' . $target);
  if ($depth > $max_depth) {
    return $result;
  }
  if ($dirs = glob($context . '/*', GLOB_ONLYDIR)) {
    foreach ($dirs as $dir) {
      $result = array_merge($result, doGlob($target, $dir, $max_depth, $depth + 1));
    }
  }
  return $result;
}
$files = doGlob('FILE.jpg', 'path_to_images', <max-depth>);

您也可以使用scandir:

function search($d)
{
    $dir=scandir($d);
    echo '<ul>';
    foreach ($dir as $key => $value) 
    {
        if($value!='.' && $value!='..')
        {
            if(is_dir($d.'/'.$value))
            {
                echo "<li>".$value."</li>";
                search($d.'/'.$value);
            }

            else
                echo "<li>".$value."</li>";

        }
    }

    echo '</ul>';
}

search('images');
函数搜索($d)
{
$dir=scandir($d);
回声“
    ”; foreach($dir作为$key=>$value) { 如果($value!='.&&$value!='..')) { if(is_dir($d./'.$value)) { 回声“
  • ”$value。”
  • ”; 搜索($d./'.$value); } 其他的 回声“
  • ”$value。”
  • ”; } } 回声“
”; } 搜索(“图像”);
在本例中,我显示所有文件,但您可以添加一个条件:

function search($d, $search)
{
    $dir=scandir($d);
    echo '<ul>';
    foreach ($dir as $key => $value) 
    {
        if($value!='.' && $value!='..')
        {
            if(is_dir($d.'/'.$value))
            {
                echo "<li>".$value."</li>";
                search($d.'/'.$value, $search);
            }

            else
            {
                echo "<li>".$value."</li>";
                if($value==$search)
                    echo "found";

            }

        }
    }

    echo '</ul>';
}

search('images', 'your_file');
函数搜索($d,$search)
{
$dir=scandir($d);
回声“
    ”; foreach($dir作为$key=>$value) { 如果($value!='.&&$value!='..')) { if(is_dir($d./'.$value)) { 回声“
  • ”$value。”
  • ”; 搜索($d./'.$value,$search); } 其他的 { 回声“
  • ”$value。”
  • ”; 如果($value==$search) 回声“发现”; } } } 回声“
”; } 搜索(‘图像’、‘您的_文件’);
到目前为止,我们尝试了什么?可能复制它可能是最好的主意,但如果我不知道它将有多少子文件夹?它可以是images/2014或images/2014/02或images/2014/02/02,等等。