Home Backend Development PHP Tutorial Detailed explanation of how to implement file search in PHP

Detailed explanation of how to implement file search in PHP

Jan 05, 2018 am 11:39 AM
php search method

How to implement file search in PHP? PHP file search program, after entering a path, it will traverse all the files and folders in the directory. Through recursion, you can find each file under the folder, and then match the file name with the entered keywords to find the file you want. Friends in need can refer to the file. I hope to be helpful.

For local, we can use the search that comes with Windows to search, but for online, such as searching for files in ftp space, this program is very useful.

php file finder source code:

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>php版文件查找(file search)</title>
 </head>
 <body>
 <form action="" method="post">
 <p> 文件查找(注:区分大小写)</p>
 <p>路径:<input type="text" name="path" /></p>
 <p>查找:<input type="text" name="key" /></p>
 <p><input type="submit" name="sub" value=" 开 始 " /></p>
 </form>
 </body>
</html>
<?php
/*
 * 注:区分大小写
 * by: http://www.daixiaorui.com
 */
if(!empty($_POST['path'])&&!empty($_POST['key'])){
 echo "在路径 ".$_POST['path']."/ 中查找 ".$_POST['key']." 的结果为:<hr/>";
 $file_num = $dir_num = 0;
 $r_file_num = $r_dir_num= 0;
 $findFile = $_POST['key'];
 function delDirAndFile( $dirName ){ 
 if ( $handle = @opendir( "$dirName" ) ) {
  while ( false !== ( $item = readdir( $handle ) ) ) { 
  if ( $item != "." && $item != ".." ) { 
   if ( is_dir( "$dirName/$item" ) ) { 
   delDirAndFile( "$dirName/$item" );
   } else { 
   $GLOBALS['file_num']++;
   if(strstr($item,$GLOBALS['findFile'])){
    echo " <span><b> $dirName/$item </b></span><br />\n";
    $GLOBALS['r_file_num']++;
   }
   } 
  }
  }
  closedir( $handle ); 
  $GLOBALS['dir_num']++;
  if(strstr($dirName,$GLOBALS['findFile'])){
  $loop = explode($GLOBALS['findFile'],$dirName);
  $countArr = count($loop)-1;
  if(empty($loop[$countArr])){
   echo " <span style='color:#297C79;'><b> $dirName </b></span><br />\n";
   $GLOBALS['r_dir_num']++;
  }
  }
 }else{
  die("没有此路径!");
 }
 }

 delDirAndFile($_POST['path']);
 echo "<hr/>本次共搜索到".$file_num."个文件,文件夹".$dir_num."个<br/>";
 echo "<hr/>符合结果的共".$r_file_num."个文件,文件夹".$r_dir_num."个<br/>";
}

?>
Copy after login

Related recommendations:

Detailed explanation How to implement a simple search box automatic prompt function in PHP

Detailed explanation of how to implement Sudoku solving in PHP

Detailed explanation of how PHP implements importing csv files into the database

The above is the detailed content of Detailed explanation of how to implement file search in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles