PHP实现递归循环每一个目录程序
要遍历一个文件夹里面的所有目录,列出里面所有的文件,PHP本身自带的有一个readdir的函数,不过只能读取当前的目录,根据这个函数,我写了另外一个函数,用来实现我的需求。
代码如下 | 复制代码 |
class listdir{ function listdir($dir){ //把结果保存进多维数组 //字符窜化结果 function tostring($dir=”"){
$this->tostring.=”Directory:”.$dir.”n”; while(false!==($item=$d->read())) { if($item!=”.”&&$item!=”..”) { $path=$dir.”/”.$item; if(is_dir($path)){ $this->depth+=1; $this->tostring($path); }else{ $this->tostring.=” } } } $this->depth-=1; $d->close(); $this->tostring.=” return $this->tostring; } } $wapdir=”jquery”; $d=new listdir($wapdir); echo $d->tostring(); ?> |
要删除一个空的目录很简单~一个
rmdir() 函数就可以搞定,但是要删除一个非空目录,将不能进行快速的删除,必须先将目录中文件删除,但是目录里可能还会有子目录所以要进行递归删除~下面是我的例子~
代码如下 | 复制代码 |
function deletedir($dir){ if(!handle=@opendir($dir)){ //检测要打开目录是否存在 die("没有该目录"); } while(false !==($file=readdir($handle))){ if($file!=="."&&$file!==".."){ //排除当前目录与父级目录 $file=$dir .DIRECTORY_SEPARATOR. $file; if(is_dir($file)){ deletedir($file); }else{ if(@unlink($file)){ echo "文件$file删除成功。 "; }else{ echo "文件$file删除失败! "; } } } if(@rmdir($dir)){ echo "目录$dir删除成功了。 n"; }else{ echo "目录$dir删除失败! n"; } } //测试程序 $dir="/var/www/test"; deletedir($dir); ?> |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment