SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot forum11's Avatar
    Join Date
    Feb 2006
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete non empty directory in PHP

    Is there a way in PHP to remove a non empty directory(folder)?
    rmdir($dir); does not remove empty directory.
    Do Good Have Good!

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could unlink any files in the directory, thus making it empty; then use rmdir.

  3. #3
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately, there is not any functionality in PHP to allow you to delete non-empty directories. However, there's a handful of examples how to recursively delete files and sub-directories using combination of ulink() and rmdir(), as @mudshark already said.
    PHP Code:
    function deleteDir($dir)
    {
       if (
    substr($dirstrlen($dir)-11) != '/')
           
    $dir .= '/';

       echo 
    $dir;

       if (
    $handle opendir($dir))
       {
           while (
    $obj readdir($handle))
           {
               if (
    $obj != '.' && $obj != '..')
               {
                   if (
    is_dir($dir.$obj))
                   {
                       if (!
    deleteDir($dir.$obj))
                           return 
    false;
                   }
                   elseif (
    is_file($dir.$obj))
                   {
                       if (!
    unlink($dir.$obj))
                           return 
    false;
                   }
               }
           }

           
    closedir($handle);

           if (!@
    rmdir($dir))
               return 
    false;
           return 
    true;
       }
       return 
    false;

    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •