【问题标题】:Search files in multiple directories using PHP使用 PHP 在多个目录中搜索文件
【发布时间】:2013-09-18 10:44:44
【问题描述】:

我前段时间写了一些脚本,基本上搜索服务器上的指定文件夹并返回该文件夹中的图像列表。该目录是通过一个变量设置的,该变量会根据您搜索的部分而改变。

但现在我需要对其进行修改,使其搜索所有目录并列出来自各个目录的图像。

这是我目前使用的脚本:

<?php
$dir = "assets/folder1/subfolder1/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(stristr($file,$_POST['image_search'])){
                echo('<li><a href="/'.$dir . $file.'">'. $file .'</a></li>'."\n");
            }
        }
        closedir($dh);
    }
}
?>

搜索 assets/folder1/ 没问题,但我也有(folder1/subfolder2、folder2/subfolder1、folder2/subfolder2/ 等)

认为必须在目录等上进行某种通配符搜索,但真的很难考虑它!

我认为像 glob 这样的东西可以提供帮助,但也不太幸运

$dir = glob("/assets/*/*/");

【问题讨论】:

标签: php opendir


【解决方案1】:

据我所知,您需要遍历所有内容并检查是否有 is_dir()。我建议为此使用对象,例如:

$dir = new Folder('/');
$contents = $dir->find('*');
foreach ($contents as $content) {
    if (is_dir($content)) {
        //do something
    }
}

你也只能让它搜索某些目录,只需修复通配符。

【讨论】:

    【解决方案2】:

    如果我理解正确,你可以使用glob函数

    $dir = '/path/to/directory/';
    $files = glob( $dir."*.{jpg,gif,png}", GLOB_BRACE );
    

    【讨论】:

      【解决方案3】:
      <?php
      /***********************************************************************
       * @name  AnyWhereInFiles
       * @author Faisal Shaikh 
       * @abstract This project is to find out a part of string from anywhere in any folder
       * @version 1.0  
       * @package anywhereinfiles
       *
       *
       *
       *
       *************************************************************************/
      session_start();
      ?>
      <title>Any where In Files || AnyWhereInFiles.php</title>
      <head>
          <style>
              h1{color:  #233E99;}
              td{ font-size:11px; font-family:arial;vertical-align:top;border:1px solid #fff;}
              a{font-size:11px; font-family:arial;}
              .me{font-size:11px; font-family:arial;color:#333;}
          </style>
      </head>
      <h1>AnyWhereInFiles.php</h1>
      <form action="<?php
      echo $_SERVER['PHP_SELF'];
      ?>" method="POST">  
          <table>
              <tbody>
                  <tr>
                      <td><label for="search">String </label></td>
                      <td><input id="search" type="text" name="search" placeholder="Enter your string" value="<?php
      if (isset($_SESSION['searchString']) && $_SESSION['searchString'] != null)
          echo $_SESSION['searchString'];
      ?>" /></td>
                  </tr>
                  <tr>
                      <td><label for="directory">Folder </label></td>
                      <td><input id="directory" type="text" name="directory"  value="<?php
      echo getcwd();
      ?>" /></td>
                  </tr>
                  <tr>
                      <td><label for="case">Case Sensitive </label></td>
                      <td><input type="checkbox" name="case" /></td>
                  </tr>
                  <tr>
                      <td><label for="maxtime">Max Execution Time </label></td>
                      <td><input type="text" name="maxtime" value="30"/></td>
                      <td>Do not change the value if you do not have an idea about it.</td>
                  </tr>
                  <tr>
                      <td><input type="submit" value="Search the string" /></td>
                  </tr>
              </tbody>
          </table>
      </form>
      
      <?php
      function getDirContents($dir, &$results = array())
      {
      
          if ($_POST['search'] == null)
              exit;
      
          ini_set('max_execution_time', $_POST['maxtime']);
      
          $_SESSION['searchString'] = $_POST['search'];
      
          echo "<script>var elm = document.getElementById('search');elm.value='$_POST[search]';</script>";
      
          if (!isset($_POST['case']))
              $string = strtolower($_POST['search']);
          else
              $string = $_POST['search'];
          $files = scandir($dir);
      
          foreach ($files as $key => $value) {
              $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
              if (!is_dir($path)) {
                  $content = file_get_contents($path);
                  if (!isset($_POST['case']))
                      $content = strtolower(file_get_contents($path));
                  if (strpos($content, $string) !== false) {
                      echo $path . "<br>";
                  }
                  $results[] = $path;
              } else if ($value != "." && $value != "..") {
                  getDirContents($path, $results);
                  $results[] = $path;
              }
          }
          return $results;
      }
      if (isset($_POST['directory']))
          getDirContents($_POST['directory']);
      //---------------------------------------------------------------------------------------------------------------------------------//
      //@endof file anywhereinfiles.php
      //@note if you have query, need, idea, help; feel free to contact f.shaikh1993@gmail.com
      ?>
      
      <br/>
      <br/>
      <span  class="me">"AnyWhereInFiles" is a Open Source Project, developed by <a href="mailto:f.shaikh1993@gmail.com">Faisal Shaikh</a> . 
      <br /> 
      <a href="https://github.com/skfaisal93/AnyWhereInFiles">https://github.com/skfaisal93/AnyWhereInFiles</a>
      </span>
      

      原项目:https://github.com/skfaisal93/AnyWhereInFiles

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多