dsgni26260 2018-08-02 05:51
浏览 61
已采纳

在目录中按名称搜索特定文件

I am creating a function for search only files inside directories and inside the folder if there is, using the recursive method. I need to list the all the files matching to my keyword. I list the all the files inside the directory and trying to match the filename to my search key, that's how I am thinking to search file, I don't know it is the correct way, even though i can list the files but the search part not working,

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $key = trim($_POST['search']);
      $path = BOOKROOT;

      if (isset($key)) {
          $books = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
          $data = array();

          foreach ($books as $book) {
              if ($book->isDir()) {
                  continue;
              }

              if ($key == $book->getFilename()) {
                  $data[] = $book->getFilename();
              }
          }
            // Load view
            $this->view('books/searchpdf', $data);
      }
}
  • 写回答

2条回答 默认 最新

  • douzai8285 2018-08-02 05:58
    关注

    In the line that you compare the name with the key, you can replace with

    if(strpos($book->getFilename(), $key) !== false) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探