Three ways to delete non-empty directories in PHP (code sharing)_PHP tutorial

WBOY
Release: 2016-07-20 11:15:08
Original
850 people have browsed it


With this small program, PHPer does not need to manually delete directory files on the computer. You can use this function when practicing PHP directory file operations. On this basis, you can still Add the browse folder directory and then delete it.

Code 1:

<?<span php
 </span><span //</span><span 删除非空目录</span>
<span function</span> d_rmdir(<span $dirname</span><span ) {
</span><span if</span>(!<span is_dir</span>(<span $dirname</span><span )) {
</span><span return</span> <span false</span><span ;
}
</span><span $handle</span> = @<span opendir</span>(<span $dirname</span><span );
</span><span while</span>((<span $file</span> = @<span readdir</span>(<span $handle</span>)) !== <span false</span><span ){
</span><span if</span>(<span $file</span> != '.' && <span $file</span> != '..'<span ){
</span><span $dir</span> = <span $dirname</span> . '/' . <span $file</span><span ;
</span><span is_dir</span>(<span $dir</span>) ? d_rmdir(<span $dir</span>) : <span unlink</span>(<span $dir</span><span );
}
}
</span><span closedir</span>(<span $handle</span><span );
</span><span return</span> <span rmdir</span>(<span $dirname</span><span ) ;
}
</span><span if</span>(d_rmdir("./temp"<span ))
</span><span echo</span> "succes"<span ;
</span><span else</span>
<span echo</span> "false"<span ;
</span><span //</span><span by www.jbxue.com</span>
?>
Copy after login

Code 2, from php Chinese online manual:

<?<span php
</span><span //</span><span 删除非空目录 
//by www.jbxue.com</span>
functionremove_directory(<span $dir</span><span ){
 </span><span if</span>(<span $handle</span>=<span opendir</span>("<span $dir</span>"<span )){
 </span><span while</span>(<span false</span>!==(<span $item</span>=<span readdir</span>(<span $handle</span><span ))){
  </span><span if</span>(<span $item</span>!="."&&<span $item</span>!=".."<span ){
   </span><span if</span>(<span is_dir</span>("<span $dir</span>/<span $item</span>"<span )){
    remove_directory(</span>"<span $dir</span>/<span $item</span>"<span );
   }</span><span else</span><span {
    </span><span unlink</span>("<span $dir</span>/<span $item</span>"<span );
    </span><span echo</span>"removing<span $dir</span>/<span $item</span><br> "<span ;
   }
  }
 }
 </span><span closedir</span>(<span $handle</span><span );
 </span><span rmdir</span>(<span $dir</span><span );
 </span><span echo</span>"removing<span $dir</span><br> "<span ;
 }
}
</span>?>
Copy after login

Code 3:

<?<span php
</span><span //</span><span 删除非空目录
//by www.jbxue.com</span>
functionremoveDir(<span $dirName</span><span )
{
  </span><span if</span>(!<span is_dir</span>(<span $dirName</span><span ))
  {
    returnfalse;
  }
  </span><span $handle</span>=@<span opendir</span>(<span $dirName</span><span );
  </span><span while</span>((<span $file</span>=@<span readdir</span>(<span $handle</span>))!==<span false</span><span )
  {
    </span><span if</span>(<span $file</span>!='.'&&<span $file</span>!='..'<span )
    {
      </span><span $dir</span>=<span $dirName</span>.'/'.<span $file</span><span ;
      </span><span is_dir</span>(<span $dir</span>)?removeDir(<span $dir</span>):@<span unlink</span>(<span $dir</span><span );
    }
  }
  </span><span closedir</span>(<span $handle</span><span );  
  returnrmdir(</span><span $dirName</span><span );
}
</span>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/440229.htmlTechArticleWith this small program, PHPer does not need to manually delete directory files on the computer. When practicing PHP directory files You can use this function during operation, and on this basis you can also add...
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