Php file name and file content finder instance-PHP source code

Source: Internet
Author: User
Searching for a file is simple. As long as you enter a directory, We will automatically traverse the directory and find the associated file name. Let's take a look at it. Searching for a file is simple. As long as you enter a directory, We will automatically traverse the directory and find the associated file name. Let's take a look at it.

Script ec (2); script

PHP file lookup program. After a path is entered, all the files and folders under the directory will be traversed, and each file under the folder can be found recursively, and then matched with the entered keyword through the file name, you can find the desired file. For local systems, we can use the search provided by windows for search. However, for online systems, such as searching for files in the ftp space, this program is very useful.


PHP file finder source code:

The Code is as follows:

File search for php)

/*

* Note: Case Sensitive

*/

If (! Empty ($ _ POST ['path']) &! Empty ($ _ POST ['key']) {

Echo "in the path". $ _ POST ['path']. "/, find". $ _ POST ['key']. "And the result is:

";

$ 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"$ DirName/$ item
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"$ DirName
N ";

$ GLOBALS ['r _ dir_num '] ++;

}

}

} Else {

Die ("this path is not available! ");

}

}


DelDirAndFile ($ _ POST ['path']);

Echo"

". $ File_num." files, Folders ". $ dir_num." are found in this query.
";

Echo"

A total of ". $ r_file_num." files and folders ". $ r_dir_num." matching results
";

}


?>

The above is just a file search. Let's see if the characters in the search file contain what we are looking.

I wrote a php program to search file content in batches. I used it to scan file signatures. Now I post the code for your reference.

The Code is as follows:


If ($ _ POST ['submit '] = 'start '){

$ Total = 0; // total number of files

$ Dangerous = array (); // dangerous File

$ Dangerous_content =$ _ POST ["sstr"];

$ Find_path = $ _ POST ["searchpath"];

$ Shortname = $ _ POST ["shortname"];

Echo"";

Echo"";

Echo"";

Echo" ";

Echo"";

Echo"";

$ Begin_time = date ("U ");

// $ Dangerous_content = "xiaoliang, Root_GP, Root_CSS, c99sh_updateurl, c99sh_sourcesurl, 640684770 ";

VisitFile ($ find_path, $ shortname );

$ End_time = date ("U ");

Foreach ($ dangerous as $ d ){

Echo $ d ."
";

}

Echo "total number of files:". $ total. "dangerous files:". count ($ dangerous). "total time". ($ end_time-$ begin_time). "seconds ";

Echo"";

Echo"";

// If (! Empty ($ dangerous )){

// Foreach ($ dangerous as $ dan ){

// Echo "[error]". $ dan ."
";

//}

//}

Exit ();

}

Function visitFile ($ path, $ ext ){

Global $ total;

Global $ dangerous_content;

$ Fdir = dir ($ path );

// Echo "Handle:". $ d-> handle ."
";

// Echo "Path:". $ fdir-> path ."
";

Set_time_limit (24*60*60 );

While ($ entry = $ fdir-> read ())! = False ){

$ PathSub = $ path. "\". $ entry;

If ($ entry! = '.' & $ Entry! = '..'){

If (is_dir ($ pathSub )){

VisitFile ($ pathSub, $ ext );

} Else {

$ Exten = explode ('.', $ entry );

$ Exten = array_reverse ($ exten); // returns the reverse order of the preceding array.

// Foreach ()

$ Shortnames = explode ('|', $ ext );

Foreach ($ shortnames as $ sn ){

If (! Empty ($ exten) & $ sn =$ exten [0]) {

$ Total = $ total + 1;

// Echo "Start analysis file:". $ path. "/". $ entry ."
";

$ Content = file_get_contents ($ path. "/". $ entry); // High Performance

$ Content = strtolower ($ content); // convert all to lowercase letters

$ Dangerous_content = strtolower ($ dangerous_content); // convert all to lower case

IsExists ($ dangerous_content, $ path. "/". $ entry, $ content); // This method is too memory-consuming. I hope some experts can solve this problem.

}

}

// Sleep (1 );

}

}

}

$ Fdir-> close ();

}

Function isExists ($ str, $ filename, $ content ){

Global $ dangerous;

// Sleep (1 );

Set_time_limit (10 );

$ Arr = explode (',', $ str );

$ Signature = "signature :";

If (! Empty ($ arr )){

// $ Content = file_get_contents ($ filename); // This has good performance.

$ Content = strtolower ($ content); // convert all to lowercase letters

$ Error_count = 0;

Foreach ($ arr as $ ){

If (trim ($ )! = ""){

If (strpos ($ content, $ )){

$ Error_count = $ error_count + 1;

$ Signature. = $ ."";

}

}

}

If ($ error_count> 0 ){

// $ Dangerous [] = $ filename;

$ Dangerous [] = "[error]". $ error_count. "". $ signature. "". $ filename;

// Echo "[error]". $ error_count. "". $ signature. "". $ filename ."
";

} Else {

// Echo "[OK]". $ filename ."
";

}

}

}

?>

Batch query of Files

Batch lookup programs

This program can scan all the files in the specified directoryContent Search.

When the number of files is very large, this operation occupies server resources. Make sure that the script time-out period can be changed. Otherwise, the operation may fail.

Cellspacing = "1" bgcolor = "#666666">

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.