search
HomeBackend DevelopmentPHP TutorialPHP rewrites the code of files in multi-level directories

  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 "
    ";
  46. print_r($option->arr_file_info);
  47. echo "";
  48. $option->rewriteFile();
  49. ?>

复制代码


Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools