如何删除服务器中所有带有文件的文件夹(cpanel)?我的错误在哪里


How to Delete all folder with file in server (cpanel) ? where is my Mistake?

此函数执行成功,但未删除任何文件夹。

public function ulink(){
      $path='/home/doman/public_html/projectname/';
      function Delete($path)
        {
     if (is_dir($path) === true)
        {
            $files = array_diff(scandir($path), array('.', '..'));
            foreach ($files as $file)
             {
                 Delete(realpath($path) . '/' . $file);
             }
             return rmdir($path);
         }
         else if (is_file($path) === true)
         {
             return unlink($path);
        }
     return false; }
   }

也可以使用此代码删除所有文件夹和子文件夹。

   function viewDir($path) {
    return is_file($path) ?
            @unlink($path) :
            array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
}
$dir=$_SERVER["DOCUMENT_ROOT"]."/xxxx/xxxx";
echo $dir;
viewDir($dir);

您可以使用此函数并根据需要更改$dir值。它对我来说很好。

function delfolder($path) {
   $files = array_diff(scandir($path), array('.','..'));
    foreach ($files as $file) {
      (is_dir("$path/$file")) ? delfolder("$path/$file") : unlink("$path/$file");
    }
    return rmdir($path);
} 

这样尝试:

<?php
/**
* Remove the directory and its content (all files and subdirectories).
* @param string $dir the directory name
*/
function rmrf($dir) {
    foreach (glob($dir) as $file) {
        if (is_dir($file)) { 
            rmrf("$file/*");
            rmdir($file);
        } else {
            unlink($file);
        }
    }
}
?>

了解更多信息:http://in3.php.net/glob