当前位置:Gxlcms > PHP教程 > php重写多级目录下的文件的代码

php重写多级目录下的文件的代码

时间:2021-07-01 10:21:17 帮助过:37人阅读

  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:\usr\www\html\test_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 "
    ";
  46. print_r($option->arr_file_info);
  47. echo "
  48. ";
  49. $option->rewriteFile();

  50. ?>

人气教程排行