PHP: Unlink All Files Within A Directory, and then

2019-01-17 03:05发布

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);

标签: php unlink rmdir
8条回答
Bombasti
2楼-- · 2019-01-17 03:57

for removing all the files you can remove the directory and make again.. with a simple line of code

<?php 
    $dir = '/home/files/';
    rmdir($dir);
    mkdir($dir);
?>
查看更多
地球回转人心会变
3楼-- · 2019-01-17 04:01

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);
}
查看更多
登录 后发表回答