Delete folder with PHP?

Discussion in 'PHP' started by scottlpool2003, Aug 8, 2011.

  1. #1
    The script I'm making is for a private gallery for a photography company. I need to add a gallery folder for individual users, but also need to delete that folder when the user is removed from the db.
     
    I can make a folder using this code:

    <?php 
    
    
     include "../includes/db.php"; //include db config file
     
    //post data from the form
    
     $username= mysql_real_escape_string($_POST['username']);
     $password= md5($_POST['password']); 
    $gallery= mysql_real_escape_string($_POST['username']); 
    
    //start insert
    
      mysql_query("INSERT INTO `p_users` VALUES ( '$id', '$username', '$password', '$gallery') "); 
    
    //done
    
      echo "<font color=green>Success:</font> The user was added to the database!<br><br><font color=red><b>YOU WILL BE REDIRECTED SHORTLY...</font></b>
    
    //redirect
    
    <meta http-equiv='REFRESH' content='3;url=http://www.***'>"; 
    
    //make the directory
    
      mkdir("../images/usergalleries/$username", 0700); ?> 
    PHP:
       But I'm having  difficulty removing the folder when the user is removed:

    <?php 
    
    $username = $_POST['username']; 
    
    include "../includes/db.php";  
    
    mysql_query("DELETE FROM p_users WHERE username = '$username'"); 
    
    echo "<font color=green>Success:</font> The user was removed from the database!<br><br><font color=red><b>YOU WILL BE REDIRECTED SHORTLY...</font></b>
    
    <meta http-equiv='REFRESH' content='3;url=http://www***'>";
    
     if (!is_dir('../images/usergalleries/$username/')) { mkdir('../images/usergalleries/$username/'); }  rmdir('../images/usergalleries/$username/'); 
    
    
    PHP:
    ?> 

    Could somebody help? If you reply with a script, could you please let me know what's happening with the script so I can understand it.

    Thanks! 
     
    scottlpool2003, Aug 8, 2011 IP
  2. MRFD

    MRFD Active Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #2
    The folder must be empty before being deleted.
    Maybe this will work (not tested).

    
    if (is_dir('../images/usergalleries/$username/')) {
    	$directory = '../images/usergalleries/$username/';
    	$handler = opendir($directory);
    	while ($file = readdir($handler)) {//delete all files
    		if ($file != "." && $file != "..") {
    			unlink($directory.$file);
    		}
    	}
    	closedir($handler);
    	rmdir($directory);
    }
    
    PHP:
     
    MRFD, Aug 8, 2011 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Nope, nada, nothing. Doesn't output an error but also doesn't remove the directory which is already empty.
     
    scottlpool2003, Aug 8, 2011 IP
  4. MRFD

    MRFD Active Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    93
    #4
    Do you have the right permissions?
     
    MRFD, Aug 8, 2011 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Ah good point, I'm sure I'm creating the directory by CHMOD 0700. 

    Edit to add:
    Nope, changed it to 0777 and still doesn't delete it. 
     
    scottlpool2003, Aug 8, 2011 IP