Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Remove directory recursively

    Hi all,

    I have a problem with a function I got from PHP.net. The function deletes the files in a directory then deletes the empty directory. My problem is that I sometimes have directories 2-levels deep and wants to delete all files and folders from a selected directory and this function will return me an object error when I do so.

    Here's the function:-

    PHP Code:
    function removedirR($conn,$dirName){
          
           
    $ar_files ftp_nlist($conn$dirName);
           if (
    is_array($ar_files)){
               for (
    $i=0;$i<count($ar_files);$i++){   
                   
    $st_file $ar_files[$i];
                   if (
    ftp_size($conn$st_file) == -1){   
                   
    $this->removedirR$st_file);
                   } else {
                   
    ftp_delete($conn$st_file);
                   }
               }
           }
           if (
    ftp_rmdir($conn$dirName))
               return 
    true;
        } 
    Can someone points me in the correct direction? I attempted using ftp_res but I'm stuck on where to add the check into the above function.

    thanks.

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've managed to get the above function to recursively delete directories and files within.

    Can someone tell me if
    (ftp_size($conn, $st_file) == -1) is a check on whether the said file is a directory? Coz I tried on one server and it actually tried to delete files when it should have deleted the directory, I got the no such directory error.

    Another server I tried, had no such problem and I could delete folders, files etc fine. Is there any additional validation I can do to check the array item whether its a file or a directory?


  •  

    Posting Permissions

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