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?
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...?
Try this one. $todelete = dir($folderlocation[0]); while($entry = $todelete->read()) { if ($entry!= "." && $entry!= "..") { unlink($folderlocation[0].'/'.$entry); } } $todelete->close(); rmdir($folderlocation[0]); PHP:
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...
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: 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.
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.
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...
ok I think I know what the problem is. Does the folder that you provide in $folderlocation[0] have sub folders?
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.