Home > Backend Development > PHP Tutorial > php file search (file search)

php file search (file search)

不言
Release: 2023-03-25 17:24:01
Original
2537 people have browsed it

<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
/*
 * 注:区分大小写
 */
if(!empty($_POST[&#39;path&#39;])&&!empty($_POST[&#39;key&#39;])){
	echo "在路径 ".$_POST[&#39;path&#39;]."/ 中查找 ".$_POST[&#39;key&#39;]." 的结果为:<hr/>";
	$file_num = $dir_num = 0;
	$r_file_num = $r_dir_num= 0;
	$findFile = $_POST[&#39;key&#39;];
	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[&#39;file_num&#39;]++;
						if(strstr($item,$GLOBALS[&#39;findFile&#39;])){
							echo " <span><b> $dirName/$item </b></span><br />\n";
							$GLOBALS[&#39;r_file_num&#39;]++;
						}
					}  
				}
			}
			closedir( $handle );  
			$GLOBALS[&#39;dir_num&#39;]++;
			if(strstr($dirName,$GLOBALS[&#39;findFile&#39;])){
				$loop = explode($GLOBALS[&#39;findFile&#39;],$dirName);
				$countArr = count($loop)-1;
				if(empty($loop[$countArr])){
					echo " <span style=&#39;color:#297C79;&#39;><b> $dirName </b></span><br />\n";
					$GLOBALS[&#39;r_dir_num&#39;]++;
				}
			}
		}else{
			die("没有此路径!");
		}
	}

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

?>
Copy after login

The above is the entire content of this article. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

php file compression package

php file upload and the basic use of uploadifive

The above is the detailed content of php file search (file search). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template