File search is to use PHP to traverse the directory and then go to a file to get the name of the comparison, and then find the file we specify to stop traversing and display the file name and address to search.
Today to do a special function to process the search file, and the search for the file may be located in a directory, look for the directory under the existence of the directory, PHP function does not seem to have a special search function.
The operation of the file in PHP is indispensable, and the frequency of the use of the file is very high, such as our common directory management system, no database messages, etc., are used to file the operation, and here the file refers to not only the operation of the directory, as well as the operation of the specified file, such as TXT file.
Here is a dedicated file search function method, File_search is the method name, followed by three parameters, $directory is the directory to search for the file, such as "admin/" directory, $search is the search for the file, $recursive is to determine whether the search is successful, by default, if you need other operations, you can add the corresponding parameters later.
Example 1
The code is as follows |
Copy Code |
function File_search ($directory, $search, $recursive = TRUE) { $res = FALSE; $dir _handle = Opendir ($directory); while (FALSE!== ($file = Readdir ($dir _handle))) { if ($file = = '. ' | | $file = = ' ... ') { Continue } if (Is_dir ("$directory \ $file")) { if ($recursive) { $res = File_search ("$directory \ $file", $search); if ($res!== FALSE) { return $res; } } } if ($file = = $search) { Return "$directory \ $file"; } } return FALSE; } ?> |
Example 2
The code is as follows |
Copy Code |
/* File Lookup function Usage: FindFile (directory, whether to traverse subdirectories, whether to find the contents of the file, not looking for directories); Ketle 2005-07-07 */ function FindFile ($dir, $find _sub_dir=false, $find _content=false, $except _dir=false) {
$d = Dir ($dir); while (false!== ($entry = $d->read ())) { if ($entry = = "." | | $entry = = ".." | | in_array ($entry, $except _dir)) Continue $file = $d->path. " /". $entry; if (Is_dir ($file)) { if ($find _sub_dir) { FindFile ($file, $find _sub_dir, $find _content, $except _dir); }
}else { if ($find _content) { if (Strstr (file_get_contents ($file), $find _content)) { echo $file. " n "; } }else { echo $file. " n "; }
} } $d->close (); }
Test FindFile ('.. ', True, ' Hibiscus JJ ', Array (' Templates_c ', ' admin ', ' Xixi ')); ?> |
Example 3
Use PHP glob function to find files, traverse files directory
Function Description: Array glob (string $pattern [, int $flags])
Function: Find the file path that matches the pattern and return an array containing matching files (directories) (Note: The file being inspected must be a server system and cannot be used for remote files)
Parameter description: First parameter: matching mode, Second optional parameter:
Glob_mark-Add a slash to each returned item
Glob_nosort-Returns (not sorted) according to the original order in which the files appear in the directory
Glob_nocheck-Returns the mode used for search if no file matches
Glob_noescape-Backslash does not escape meta-characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only catalog entries that match the pattern
The code is as follows |
Copy Code |
$file = Glob (' {,.} * ', glob_brace); Match All Files $file 1 = glob (' *.php '); Match all PHP files Print_r ($file 1);
|
Example 4
The code is as follows |
Copy Code |
Print_r (Listdir ('./')); Traverse the current directory function Listdir ($dir) { $dir. = substr ($dir,-1) = = '/'? '' : '/'; $dirInfo = Array (); foreach (Glob ($dir. ' * ') as $v) { $dirInfo [] = $v; if (Is_dir ($v)) { $dirInfo = Array_merge ($dirInfo, Listdir ($v)); } } return $dirInfo; } |
http://www.bkjia.com/PHPjc/632907.html www.bkjia.com true http://www.bkjia.com/PHPjc/632907.html techarticle file search is to use PHP to traverse the directory and then go to a file to get the filename for comparison, and then find the file we specified stop traversing and display the file name and address to search ...