Using PHP To Remove Folder And Entire Contents- Help Needed

Discussion in 'PHP' started by j0563, Jun 2, 2010.

  1. #1
    I am trying to have my script folder a directory and all it's contents.

    The folder I am trying to delete is defined as $folderlocation[0].

    Here's what I have so far:

    $todelete = dir($folderlocation[0]); 
    while($entry = $todelete->read()) { 
     if ($entry!= "." && $entry!= "..") { 
     unlink($entry); 
     } 
    } 
    $todelete->close(); 
    rmdir($folderlocation[0]);
    PHP:
    If the folder is empty, everything works fine and the folder is deleted, but if there are any contents within the folder, I will get an error message like this:

    Warning: unlink(filename.txt) [function.unlink]: No such file or directory in /path/to/myscript.php
    Code (markup):
    It is identifying each file & folder within the folder I am trying to remove, but something is still wrong. Could someone help me out a bit please?
     
    j0563, Jun 2, 2010 IP
  2. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    NOTE: I'm beginning to think that the fact that my folder to delete is defined as an array: $folderlocation[0] is hurting me. Maybe there is some way to change it from an array to a simple string...?
     
    Last edited: Jun 2, 2010
    j0563, Jun 2, 2010 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    Try this one.

    
    
    $todelete = dir($folderlocation[0]);
    while($entry = $todelete->read()) {
     if ($entry!= "." && $entry!= "..") {
     unlink($folderlocation[0].'/'.$entry);
     }
    }
    $todelete->close();
    rmdir($folderlocation[0]);
    
    
    PHP:
     
    stephan2307, Jun 2, 2010 IP
  4. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for your help... I tried this and it works only if the folder is empty. If there are any files inside, I get a "directory not empty" error and if it is a folder, I get a "Is a directory" error...


     
    j0563, Jun 2, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    OK just tried it and this code works

    
    $todelete = dir($folderlocation[0]);
    while($entry = $todelete->read()) {
     if ($entry!= "." && $entry!= "..") {
     unlink('/'.$folderlocation[0].'/'.$entry);
     }
    }
    $todelete->close();
    rmdir($folderlocation[0]);
    
    PHP:
    If you are still having problems then it is a permission problem.
     
    stephan2307, Jun 2, 2010 IP
  6. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    stephan2307: Yeah, I am getting the same error. "Directory not empty" and "Is a directory" errors. :(

    I have been trying a lot of different ways to try to delete a folder and it's contents via PHP- there are quite a few examples out there- but none of them seem to be working for me.

    The one hangup that I have is that the directory is not exactly the same every time the script is run. That's why the directory location must be defined as $folderlocation[0], which is a home folder location pulled from the user's table in the database. If I simply echo $folderlocation[0], the correct location appears, so I know it's value is correct.
     
    j0563, Jun 2, 2010 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    If you are allowing your script to have those sort of permissions, why don't you execute a shell script instead?

    You can run an exec, "rm -rf foldername" to dump the whole thing.
     
    jestep, Jun 2, 2010 IP
  8. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    To get this to work well with the recursive unlinking code I've been seeing, I have been trying to convert the SQL array (which is only one result- the user's home folder) to a simple string. Here's what I have:

    
    	$homefolder = mysql_query("SELECT homefolder FROM df_users_permissions WHERE id=$docsuserid"); 
    
        $folderlocation = mysql_fetch_array($homefolder);
    
    
    echo "Home folder is: $folderlocation[0] <br /><br /><br />";
    
    // At this point, the folder location is displayed correctly. However, as I convert this to a string:
    
    // IMPLODE FUNCTION
    
    $new = implode("",$folderlocation);
    
    echo "The new home folder is:<br /><br />";
    echo $new;
    echo "<br /><br />";
    
    // Imploding this to a single string displays the home folder two times in a row instead of just once
    
    //END IMPLODE FUNCTION 
    
    PHP:
    As you can see in my notes, when I implode the array to a string, the home folder is shown two times in a row- like this:

    /home/user/public_html/folder/home/user/public_html/folder

    I can't understand why this is displaying twice. If I can get this to work, I have a lot of code examples I can use in order to get the folder and it's files removed...
     
    j0563, Jun 2, 2010 IP
  9. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #9
    ok I think I know what the problem is. Does the folder that you provide in $folderlocation[0] have sub folders?
     
    stephan2307, Jun 4, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    $folderlocation[0] is your Root or Main Dir
     
    roopajyothi, Jun 4, 2010 IP
  11. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #11
    As you can see from post #8 your statement is not simply not true.
     
    stephan2307, Jun 4, 2010 IP
  12. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #12
    Right I have written a recursive function that deletes all files and folders even if you have sub folders that have sub folders with sub folders in them

    
    
    function deleteFolder($folder) {
    	
        $todelete = dir($folder);
        
        while($entry = $todelete->read()) {
            if ($entry!='.' && $entry!='..') {
                if(is_dir($folder.'/'.$entry)) {
                    deleteFolder($folder.'/'.$entry);
                }
                if (is_file($folder.'/'.$entry)) {
                    unlink($folder.'/'.$entry);
                }
            }
        }
        
        $todelete->close();
        rmdir($folder);    
    }
    
    
    PHP:
    And you simply call it like this

    
    deleteFolder($folderlocation[0]);
    
    PHP:
    All you need to make sure is that the path you pass in (that is stored in $folderlocation[0]) is correct.
     
    stephan2307, Jun 4, 2010 IP
    j0563 likes this.
  13. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    That's great! Thanks so much... +rep!
     
    j0563, Jun 4, 2010 IP
  14. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #14
    Did it work for you?
     
    stephan2307, Jun 4, 2010 IP
  15. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #15
    I'm guessing from his reply that it did.
     
    HuggyEssex, Jun 4, 2010 IP