PHP File Search program code

Source: Internet
Author: User
Tags glob php file

Today to do a special function to deal with search files, and the search file may be located in a directory, looking for the directory exists in the directory, PHP functions do not seem to have a special search function.

The operation of the file is indispensable in PHP, and the use of the file frequency is very high, such as our common directory management system, no database messages, etc., have used the operation of the file, 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 function method dedicated to file search, File_search is the method name, followed by three parameters, $directory is the directory to search for files, such as the "admin/" directory, $search is the search file, $recursive is to determine whether the search is successful, the default is OK, if you need other operations, you can add the corresponding parameters in the following.

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 Lookup function
Usage:
FindFile (directory, whether to traverse subdirectories, whether to find the contents of the file, not lookup directory);
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. " <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 find files, traverse the file directory

Function Description: Array glob (string $pattern [, int $flags])
Function: Find a file path that matches the pattern, and return an array containing matching files (note: The file being checked must be a server system and cannot be used for remote files)
Parameter description: First parameter: match 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 pattern for searching if no file match
Glob_noescape-Backslash does not escape meta characters
Glob_brace-expand {a,b,c} to match ' A ', ' B ' or ' C '
Glob_onlydir-Returns only directory 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

  code is as follows copy code

<?php

Print_r (Listdir ('./'));//traverse 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.