【问题标题】:delete directories and files including subdirectories and files in subdirectories (php)删除目录和文件,包括子目录和子目录中的文件(php)
【发布时间】:2017-10-08 21:16:50
【问题描述】:

如何删除文件夹one中的所有文件和目录。以下是我的文件夹结构:

删除.php

<?php

    function rrmdir($dir) { 
      $dir = 'C:xampp/htdocs/project/user/one';
       if (is_dir($dir)) {
         $objects = scandir($dir); 
         foreach ($objects as $object) { 
           if ($object != "." && $object != "..") { 
             if (is_dir($dir."/".$object)){
               rrmdir($dir."/".$object);
             }
             else{
               unlink($dir."/".$object); 
             }
           } 
         }
         rmdir($dir); 
       } 
    }

    ?>

我已经尝试了从here 获得的代码,但代码没有做任何事情。好像该功能不起作用。

【问题讨论】:

标签: php recursion delete-directory


【解决方案1】:

这将递归删除您的文件。它可以正常工作,并确保在删除文件之前备份您的文件。 这里我们使用glob函数递归删除文件。

<?php

ini_set('display_errors', 1);
function delete($filePath,$array=array())
{
    if(is_array($array) && count($array)>0)
    {
        foreach($array as $filePath)
        {
            if(is_dir($filePath))
            {
                delete(glob($filePath."/*"));//first calling function itself to remove files first.
                rmdir($filePath);//removing directory at the end.
            }
            else    
            {
                unlink($filePath);//unlinking a file.
            }   
        }
    }

}
print_r(delete(glob("C:xampp/htdocs/project/user/one/*")));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-02
    • 2023-01-04
    • 2021-10-30
    • 2014-09-03
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多