PHP rewrites the code of files in multi-level directories

WBOY
Release: 2016-07-25 08:51:34
Original
821 people have browsed it
  1. // FileName: rewrite.php

  2. // 功能: 重写xxx目录下所有的htm文件(也可以是php文件)
  3. // Author: windlike.cublog.cn

  4. function getFileInfo($dir, $type){

  5. global $arr_file;
  6. $mydir = dir($dir);
  7. while(false !== ($file = $mydir->read())){
  8. if((is_dir("$dir/$file")) && ($file != ".") && ($file != "..")){
  9. getFileInfo("$dir/$file",$type);
  10. }else{
  11. if(($file != ".") && ($file != "..")){
  12. $path_info = pathinfo("$file");
  13. if($path_info["extension"] == $type){
  14. $arr_file["$dir"][] = $file;
  15. }
  16. }
  17. }
  18. }
  19. $mydir->close();
  20. }

  21. function Rewrite_File($content){

  22. global $arr_file;
  23. foreach($arr_file as $key=>$arr){
  24. foreach($arr as $value){
  25. $file = $key . '/' . $value;
  26. $fp = fopen($file, 'w');
  27. fwrite($fp, $content);
  28. fclose($fp);
  29. }
  30. }
  31. }

  32. //

  33. $dir = "xxx";
  34. $type = "htm";
  35. $content = "hello world!n";
  36. getFileInfo($dir, $type);
  37. Rewrite_File($content);
  38. ?>

复制代码

以上文件在我的电脑上已经通过测试。为了使用起来更方便,我试着写了一个Rewrite类,可是运行时总是提示错误: Fatal error: Call to undefined function: getfileinfo() in d:usrwwwhtmltest_class.php on line 24

刚接触php类,路过的朋友帮我看一下是哪里的错误。

  1. // FileName: test_class.php

  2. class Rewrite_File{
  3. var $file_dir;
  4. var $file_content;
  5. var $file_type;
  6. var $arr_file_info;
  7. function Rewrite_File($file_dir, $file_content, $file_type){
  8. $this->file_dir = $file_dir;
  9. $this->tem_file_dir = $file_dir;
  10. $this->file_content = $file_content;
  11. $this->file_type = $file_type;
  12. }
  13. function getFileInfo(){
  14. $dir = $this->tem_file_dir;
  15. $mydir = dir($dir);
  16. while(false !== ($file = $mydir->read())){
  17. if((is_dir("$dir/$file")) && ($file != ".") && ($file != "..")){
  18. //

  19. $this->tem_file_dir = $dir;

  20. getFileInfo();
  21. }else{
  22. if(($file != ".") && ($file != "..")){
  23. $path_info = pathinfo("$file");
  24. if($path_info["extension"] == $this->file_type){
  25. $this->arr_file_info["$dir"][] = $file;
  26. }
  27. }
  28. }
  29. }
  30. $mydir->close();
  31. }
  32. function rewriteFile(){
  33. foreach($this->arr_file_info as $key=>$arr){
  34. foreach($arr as $value){
  35. $file = $key . '/' . $value;
  36. $fp = fopen($file, 'w');
  37. fwrite($fp, $this->content);
  38. fclose($fp);
  39. }
  40. }
  41. }

  42. }

  43. $option = new Rewrite_File("xxx","hello
    nworld","htm");

  44. $option->getFileInfo();
  45. echo "
    ";</li>
    <li>print_r($option->arr_file_info);</li>
    <li>echo "
    ";
  46. $option->rewriteFile();

  47. ?>

复制代码


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