PHP遍歷目錄下的文件夾和文件以及遍歷文件下內容


1.遍歷目錄下的文件夾和文件:

 1 public function bianli1($dir)
 2     {
 3         $files = array();
 4         if($head = opendir($dir))
 5         {
 6             while(($file = readdir($head)) !== false)
 7             {
 8                 if($file != ".." && $file!=".")
 9                 {
10                    if(is_dir($file))
11                    {
12                        $files[$file]=bianli1($dir.'/'.$file);
13                    }
14                    else
15                    {
16                        $files[]=$file;
17                    }
18                 }
19             }
20         }
21         closedir($head);
22         return $files;
23     }

2.遍歷文件下內容:

 1 function bianli2()
 2     {
 3         $hostdir= iconv("utf-8","gbk","D:/wamp/www") ;
 4         $filesnames = scandir($hostdir);
 5         foreach ($filesnames as $name) {
 6             if($name!=".." && $name!=".")
 7             {
 8                 $cipath = $hostdir.$name;
 9                 $cjfilenames = scandir($cipath);
10                 foreach($cjfilenames as $cjname)
11                 {
12                     if($cjname!=".." && $cjname!="." &&!is_dir($cipath."/".$cjname))
13                     {
14                         $str = file_get_contents($cipath."/".$cjname);
15                         file_put_contents("D:/test.txt",$str,FILE_APPEND);
16                     }
17                 }
18             }
19             echo "\n";
20         }
21     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM