PHP:取消链接目录中的所有文件,然后删除该目录 [英] PHP: Unlink All Files Within A Directory, and then Deleting That Directory

查看:109
本文介绍了PHP:取消链接目录中的所有文件,然后删除该目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以使用RegExp或通配符搜索来快速删除文件夹中的所有文件,然后在PHP中删除该文件夹,而无需使用"exec"命令?我的服务器未授权我使用该命令.一个简单的循环就足够了.

I there a way I can use RegExp or Wildcard searches to quickly delete all files within a folder, and then remove that folder in PHP, WITHOUT using the "exec" command? My server does not give me authorization to use that command. A simple loop of some kind would suffice.

我需要能够完成以下语句背后的逻辑的东西,但显然是有效的:

I need something that would accomplish the logic behind the following statement, but obviously, would be valid:



$dir = "/home/dir"
unlink($dir . "/*"); # "*" being a match for all strings
rmdir($dir);

推荐答案

使用 glob 进行找到所有与模式匹配的文件.

Use glob to find all files matching a pattern.

function recursiveRemoveDirectory($directory)
{
    foreach(glob("{$directory}/*") as $file)
    {
        if(is_dir($file)) { 
            recursiveRemoveDirectory($file);
        } else {
            unlink($file);
        }
    }
    rmdir($directory);
}

这篇关于PHP:取消链接目录中的所有文件,然后删除该目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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