Find out if a file contains a specific string in PHP

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags *.h file check class continue date directory display echo

There's a whole bunch of files. How do we find what we need, and the way to do that is to find the corresponding directory. Open the directory. Traverse all files. Each file is scanned again if it contains a display and does not continue looking until the end.

Paging File (allfile.php)


<div id= "Checkarea" >
<h2>just Check it!</h2>
<form action= "check.php" method= "POST" >
Input the folder ' s relative path:<br/>
<input type= "text" name= "Checkdir"/><br
Input What you wanna check:<br/>
<input type= "text" name= "Checkstr"/><br
<input type= "Submit" value= "check!" class= "btn"/>
</form>
</div>
Content Processing file (check.php)




&lt;?php


//Get to the page to pass the file path, need to query the string


//Remove all spaces in the string, enter, tab indent


$dir = $_post["Checkdir"];


$check = $_post["Checkstr"];


 


$check = Str_replace ("", "", $check);


$check = Str_replace ("rn", "", $check);


$check = str_replace ("T", "", $check);


 


//Open Directory


$dir _handle = Opendir ($dir);


$date = "";


 


echo "&lt;table id=" Allfile "&gt;n";


echo "&lt;tr&gt;&lt;th class=" ListNum &gt;num&lt;/th&gt;&lt;th&gt;stauts&lt;/th&gt;&lt;th class= "AlignLeft" &gt;file name&lt;/th&gt;&lt;th&gt;file Size&lt;/th&gt;&lt;th&gt;file type&lt;/th&gt;&lt;th&gt;time&lt;/th&gt;&lt; /tr&gt; ";


 


$listNum = 0;


while ($file = Readdir ($dir _handle)) {


if (filetype ($dir. $file)!= "dir") {


$listNum + +;


 


echo "&lt;tr&gt;&lt;td class=" ListNum "&gt;". $listNum. " &lt;/td&gt; ";


 


//Get all the contents of the file (string)


$date = file_get_contents ($dir. $file);


 


//Remove all spaces in the string, enter, tab indent


$date = Str_replace ("", "", $date);


$date = Str_replace ("rn", "", $date);


$date = str_replace ("T", "", $date);


 


//Detect if a specific string is included


if (strstr ($date, $check)) {


echo "&lt;td&gt;&lt;span&gt;√&lt;/span&gt;&lt;/td&gt;";


}else{


echo "&lt;td&gt;&lt;span class=" Red "&gt;X&lt;/span&gt;&lt;/td&gt;";


   }


$date = "";


 


//Print results


echo "&lt;td class=" alignleft "&gt;&lt;a href=" "". $dir. $file. "target=" _blank "&gt;". $file. " &lt;/a&gt;&lt;/td&gt; ";


echo "&lt;td&gt;" FileSize ($dir. $file). "Byte&lt;/td&gt;";


echo "&lt;td&gt;" filetype ($dir. $file). &lt;/td&gt; ";


echo "&lt;td&gt;". Date ("Y-n-t", (Filemtime ($dir. $file)). " &lt;/td&gt; ";


echo "&lt;/tr&gt;n";


  }


 }


echo "&lt;/table&gt;n&lt;/div&gt;";


 


//Close directory


Closedir ($dir _handle);


?&gt;


style sheet files are not written. You need to change it as you want.

Related Article

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.