\n"; $i=1; while($ ..."/> \n"; $i=1; while($ ...">

Home  >  Article  >  Backend Development  >  php traverses a directory and renames it

php traverses a directory and renames it

巴扎黑
巴扎黑Original
2016-11-22 09:36:25962browse

/**********************
一个简单的目录递归函数
第一种实现办法:用dir返回对象
***********************/
function tree($directory) 
{ 
 $mydir = dir($directory); 
 echo "<ul>\n"; 
        $i=1;
 while($file = $mydir->read())
 { 
  if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) 
  {
                    
   echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n"; 
   tree("$directory/$file"); 
  } 
  else {
                    if(($file!=".") AND ($file!="..")) {
                        rename("/wamp5_3_2/www/m2_old/tietu/".$file, "/wamp5_3_2/www/m2_old/tietu/".$i.&#39;.png&#39;);
                    $i++; 
                    }
                   
                    echo "<li>$file</li>\n"; 
                }
 } 
 echo "</ul>\n"; 
 $mydir->close(); 
} 
//开始运行
echo "<h2>目录为粉红色</h2><br>\n"; 
tree("/wamp5_3_2/www/m2_old/tietu"); 
exit;
/***********************
第二种实现办法:用readdir()函数
************************/
function listDir($dir)
{
 if(is_dir($dir))
    {
      if ($dh = opendir($dir)) 
  {
         while (($file = readdir($dh)) !== false)
   {
        if((is_dir($dir."/".$file)) && $file!="." && $file!="..")
    {
         echo "<b><font color=&#39;red&#39;>文件名:</font></b>",$file,"<br><hr>";
         listDir($dir."/".$file."/");
        }
    else
    {
            if($file!="." && $file!="..")
     {
             echo $file."<br>";
          }
        }
         }
         closedir($dh);
      }
    }
}
//开始运行
listDir("");


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