PHP file search program code

Source: Internet
Author: User
Tags glob

File search is to use php to traverse the directory and then retrieve the file names from each file for comparison. Then, when we find the specified file, we will stop traversing and display the file name and address to be searched.

Today, we will create a function dedicated to processing the search file, and the search file may be located in a directory to find whether the directory exists, PHP functions do not seem to have such search functions.

File Operations are indispensable in php, and files are frequently used. For example, file operations are used in common directory management systems without database message books, files here refer to not only directory operations, but also operations on specified files, such as txt files.

The following is a function method specifically used for file search. file_search is the method name, followed by three parameters. $ directory is the directory of the file to be searched, for example, in the "admin/" directory, $ search is the file to be searched, and $ recursive is used to determine whether the search is successful. By default, you can perform other operations, you can add parameters later.

Example 1

The Code is as follows: Copy code

<? Php

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

<? Php
/* File search function
Usage:
Findfile (directory, whether to traverse sub-directories, whether to find file content, not Directories );
Ketle
2005-07-07
*/
Function findfile ($ dir, $ find_sub_dir = false, $ find_content = false, $ effect_dir = false)
{

$ D = dir ($ dir );
While (false! ==( $ Entry = $ d-> read ())){
If ($ entry = "." | $ entry = ".." | in_array ($ entry, $ effect_dir ))
Continue;
$ File = $ d-> path. "/". $ entry;
If (is_dir ($ file ))
{
If ($ find_sub_dir)
{
Findfile ($ file, $ find_sub_dir, $ find_content, $ effect_dir );
}

} Else
{
If ($ find_content)
{
If (strstr (file_get_contents ($ file), $ find_content ))
{
Echo $ file. "<br> n ";
}
} Else
{
Echo $ file. "<br> n ";
}

}
}
$ D-> close ();


}

// Test:
Findfile ('..', true, 'hibiscus jj ', array ('templates _ C', 'admin', 'xixi '));
?>

Example 3

Use the php glob function to search for a file and traverse the file directory.

Function Description: array glob (string $ pattern [, int $ flags])
Function: Find the file path that matches the pattern and return an array containing the matched file (directory). Note: The file to be checked must be from the server system and cannot be used for remote files)
Parameter description: The first parameter: matching mode; the second parameter is optional:


• GLOB_MARK-Add a diagonal line to each returned item
• GLOB_NOSORT-returns (unordered) files in the original order in the directory)
• GLOB_NOCHECK-returns the search mode if no matching file exists.
• GLOB_NOESCAPE
• GLOB_BRACE-extended {a, B, c} to match 'A', 'B' or 'C'
• GLOB_ONLYDIR-returns only directory items that match the pattern.

The Code is as follows: Copy code


$ File = glob ('{,.} *', GLOB_BRACE); // match all files
$ File1 = glob ('*. php'); // match all php files
Print_r ($ file1 );

Example 4

The Code is as follows: Copy code

<? Php

Print_r (listDir ('./'); // traverses 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.