Php file name and file content finder instance

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.

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: Copy code

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> file search for php </title>

</Head>

<Body>

<Form action = "" method = "post">

<P> file search (Note: Case Sensitive) </p>

<P> path: <input type = "text" name = "path"/> </p>

<P> Search: <input type = "text" name = "key"/> </p>

<P> <input type = "submit" name = "sub" value = "start"/> </p>

</Form>

</Body>

</Html>

<? Php

/*

* Note: Case Sensitive

*/

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

Echo "in path ". $ _ POST ['path']. "/In Search ". $ _ POST ['key']. "Result:

$ 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 "<span> <B> $ dirName/$ item </B> </span> <br/> 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 "<span style = 'color: #297C79; '> <B> $ dirName </B> </span> <br/> n ";

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

}

}

} Else {

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

}

}

 


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

Echo "

Echo "

}

 


?>

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: Copy code


<? Php

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 "<! DOCTYPE html PUBLIC '-// W3C // dtd xhtml 1.0 Transitional // en' 'HTTP: // www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> ";

Echo "

Echo "

Echo "<meta http-equiv = 'content-type' Content = 'text/html; charset = UTF-8 '/> ";

Echo "

Echo "<body> ";

$ 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. "<br/> ";

}

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

Echo "</body> ";

Echo "

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

// Foreach ($ dangerous as $ dan ){

// Echo "[error]". $ dan. "<br/> ";

//}

//}

Exit ();

}

Function visitFile ($ path, $ ext ){

Global $ total;

Global $ dangerous_content;

$ Fdir = dir ($ path );

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

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

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. "<br> ";

$ 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. "<br/> ";

} Else {

// Echo "[OK]". $ filename. "<br/> ";

}

}

}

?>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> Query files in batches </title>

<Style type = "text/css">

Body {

Background: # FFFFFF;

Color: #000;

Font-size: 12px;

}

 

# Top {

Text-align: center;

}

 

H1, p, form {

Margin: 0;

Padding: 0;

}

 

H1 {font-size; 14px;

 

}

</Style>

</Head>

<Body>

<Div id = "top">

<H1> batch search program

<Div> This program can scan all the files in the specified directory and perform <strong> Content Search </strong>. <Br/>

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. </Div>

</Div>

 

 

<Form action = "<? = $ _ SERVER ['script _ name']?> "Name =" form1"

Target = "stafrm" method = "post">

<Table width = "95%" border = "0" align = "center" cellpadding = "3"

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

<Tr>

<Td width = "10%" bgcolor = "# FFFFFF"> <strong> & nbsp; Start root path: </strong> </td>

<Td width = "90%" bgcolor = "# FFFFFF"> <input name = "searchpath" type = "text"

Id = "searchpath" value = "D:/" size = "20"/> click to indicate the current directory. Do not add/at the end. </td>

</Tr>

<Tr>

<Td bgcolor = "# FFFFFF"> <strong> & nbsp; File Extension: </strong> </td>

<Td bgcolor = "# FFFFFF"> <input name = "shortname" type = "text"

Id = "shortname" size = "20" value = "htm | html | shtml | php"/> separate multiple values with | </td>

</Tr>

<Tr id = "rpct">

<Td height = "64" colspan = "2" bgcolor = "# FFFFFF">

<Table width = "100%" border = "0" cellspacing = "1" cellpadding = "1">

<Tr bgcolor = "# EDFCE2">

<Td colspan = "4"> <strong> content search options: </strong> <input type = "checkbox"

Name = "isreg" value = "1"/> use regular expressions </td>

</Tr>

<Tr>

<Td colspan = "4"> by default, the search content class uses string search, or you can use a regular expression (check required ). "If this parameter is left blank, the system deletes the" search content ".

<Br/> com, system, exec, eval, escapeshell, cmd, passthru, base64_decode, gzuncompress

</Td>

</Tr>

<Tr>

<Td width = "10%"> & nbsp; search content: </td>

<Td width = "36%" colspan = "3"> <textarea name = "sstr" id = "sstr"

Style = "width: 90%; height: 45px"> Xiao Liang, Root_GP, Root_CSS, c99sh_updateurl, c99sh_sourcesurl, 640684770, hx_dealdir, while (1) </textarea> </td>

</Tr>

</Table>

</Td>

</Tr>

<Tr>

<Td colspan = "2" height = "20" align = "center" bgcolor = "# E2F5BC"> <input

Type = "submit" name = "Submit" value = "" class = "inputbut"/> </td>

</Tr>

</Table>

</Form>

<Table width = "95%" border = "0" align = "center" cellpadding = "3"

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

<Tr bgcolor = "# FFFFFF">

<Td id = "mtd">

<Div id = 'magrou' style = 'width: 100%; height: 100; '> <iframe name = "stafrm"

Frameborder = "0" id = "stafrm" width = "100%" height = "100%"> </iframe> </div>

<Script type = "text/javascript">

Document. all. DV. style. pixelHeight = screen. height-450;

</Script> </td>

</Tr>

</Table>

</Body>

</Html>

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.