Home > Backend Development > PHP Tutorial > PHP code that writes content in the specified line of the file

PHP code that writes content in the specified line of the file

WBOY
Release: 2016-07-25 09:05:20
Original
1175 people have browsed it
  1. $file="aa.php" ;
  2. $code="";
  3. $ f=fopen($file,"r+");
  4. $content=fread($f,filesize($file));
  5. fclose($f);
  6. if(!strstr($content,$code)){
  7. $arrInsert = insertContent($file, $code, 3);
  8. unlink($file);
  9. foreach($arrInsert as $value)
  10. {
  11. file_put_contents($file, $value, FILE_APPEND);
  12. }
  13. }
  14. function insertContent($source, $s, $iLine) {
  15. $file_handle = fopen($source, "r");
  16. $i = 0;
  17. $arr = array();
  18. while (!feof($file_handle)) {
  19. $line = fgets($file_handle);
  20. ++$i;
  21. if ($i == $iLine) {
  22. $arr[] = $line .$s . "n";
  23. }else {
  24. $ arr[] = $line;
  25. }
  26. }
  27. fclose($file_handle);
  28. return $arr;
  29. }
  30. ?>
Copy the code

and save it as demo.php, and then use a small program to separate it It only takes a few minutes to execute it. Browsers like Opera have this function. But I added another html code to run it. You can use IE. You can just open the html and throw it wherever you want.

Copy code

Instructions: These codes will be written in php, and you can write a rule to prohibit modification of php files.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template