PHP的递归删除目录函数? [英] A recursive remove directory function for PHP?

查看:87
本文介绍了PHP的递归删除目录函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP移动图片子文件夹的内容

I am using PHP to move the contents of a images subfolder


GalleryName / images /

GalleryName/images/

到另一个文件夹。移动之后,我需要删除GalleryName目录和其中的所有内容

into another folder. After the move, I need to delete the GalleryName directory and everything else inside it.

我知道 rmdir()将无法正常工作,除非目录为空。我花了一段时间尝试从顶部开始向 scandir()构建一个递归函数,然后从$ code> unlink()如果它是一个文件,而$ code> scandir()如果它是一个目录,那么当我去的时候,每个空的目录 rmdir()

I know that rmdir() won't work unless the directory is empty. I've spent a while trying to build a recursive function to scandir() starting from the top and then unlink() if it's a file and scandir() if it's a directory, then rmdir() each empty directory as I go.

到目前为止,它的工作不正确,我开始认为 - 这不是PHP应该能够做的一个荒谬的简单功能吗? / strong>删除一个目录?

So far it's not working exactly right, and I began to think -- isn't this a ridiculously simple function that PHP should be able to do? Removing a directory?

那么我有没有丢失的东西?或者至少有一个被证明的功能,人们用于这个行动?

So is there something I'm missing? Or is there at least a proven function that people use for this action?

任何帮助将不胜感激。

PS我相信你们在这里的所有评论php.net网站 - 有数百个功能,但我有兴趣听到,如果你们中的任何人推荐一个人。

推荐答案

这个问题呢?

foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
        $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
}
rmdir($dirPath);

这篇关于PHP的递归删除目录函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆