kev wood Posted May 22, 2008 Share Posted May 22, 2008 where can i find a tutorial on removing folders from the server with php? i have been googling it but had no joy Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 22, 2008 Share Posted May 22, 2008 I believe this will do the trick. Just be careful when your playing with it. // Path to directory you want to delete $directory = 'dir'; // Delete it if (rmdir($directory)) { echo "{$directory} has been deleted"; } else { echo "{$directory} could not be deleted"; } Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 22, 2008 Share Posted May 22, 2008 I just tried the above code and even with a folder CHMOD of 777, it doesn't work, so I'll let someone else pick up the ball on this one. I'm pretty sure it involves using rmdir though. Warning: rmdir(/home/user/public_html/test/asd/) [function.rmdir]: Permission denied in /home/user/public_html/test/test.php on line 7 /home/user/public_html/test/asd/ could not be deleted Quote Link to comment Share on other sites More sharing options...
kev wood Posted May 22, 2008 Author Share Posted May 22, 2008 thanks for the reply. i was reading somewhere that the rmdir function will not delete the folder if it has files held inside? Quote Link to comment Share on other sites More sharing options...
kev wood Posted May 22, 2008 Author Share Posted May 22, 2008 i just checked again the folder must be empty for rmdir to delete the folder. ??? Quote Link to comment Share on other sites More sharing options...
haku Posted May 22, 2008 Share Posted May 22, 2008 I believe unlink() should do the trick. Quote Link to comment Share on other sites More sharing options...
gamefreak13 Posted May 22, 2008 Share Posted May 22, 2008 thanks for the reply. i was reading somewhere that the rmdir function will not delete the folder if it has files held inside? My test was done with an empty folder (asd) which was inside the folder test. Quote Link to comment Share on other sites More sharing options...
kev wood Posted May 22, 2008 Author Share Posted May 22, 2008 dont no why that didnt work on a empty folder according to the php manual it only works on empty folders and the unlink must be used first to empty the folder. i was looking at the unlink function earlier but some of the files have had random numbers appended to them so to get the unlink file to work i need to know the full file name first to get it to work i think. i may be wrong tho i will go check that out now. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 22, 2008 Share Posted May 22, 2008 Indeed. You can only delete a directory once it is empty; you will have to remove all the files first. If there are no subdirectories in the folder you wish to delete, you could use: <?php $dir = 'test2/'; foreach(glob($dir.'*.*') as $v){ unlink($v); } rmdir($dir); ?> Otherwise, you'll have to write a recursive function (examples of which can be found on the user contributed notes in the manual here or here) Quote Link to comment Share on other sites More sharing options...
mlin Posted May 22, 2008 Share Posted May 22, 2008 you could try: exec('rm -fr ' . $dir); this is a basic shell cmd so if your install has shell access and permissions to the dir, your set...otherwise, your sol rm = remove -fr are flags for the cmd. f = force, r = recursive hope this helps. Quote Link to comment Share on other sites More sharing options...
kev wood Posted May 22, 2008 Author Share Posted May 22, 2008 gingerobot am i right in thinking that this will delete all files from within the directory and then remove the directory. from what i have gathered it looks for files with a . in them and removes these. mlin thanks for your reply but i know a little bit about the reply before yours so i will look at going down this road for now Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 22, 2008 Share Posted May 22, 2008 Indeed. It will match anything of the form *.* --basically all files, no directories. Quote Link to comment Share on other sites More sharing options...
mlin Posted May 22, 2008 Share Posted May 22, 2008 cool, I like to stick with what I know as well. Ginger's solution will work for sure as long as there are no subdir's in the folder as she said. Add recursion to her solution and subdir's won't matter either Quote Link to comment Share on other sites More sharing options...
kev wood Posted May 22, 2008 Author Share Posted May 22, 2008 i changed the code ever so slightly to work exactly how i wanted. i need it for an image upload folder. when an image is uploaded a thumb nail is created and the original image are both stored on the server in there respective folders. i need to give the user the option to remove all the file that had been uploaded and this code does exactly what i needed it to. thank you to all who replied. here is the final code i used $dir = 'image/thumbs/'; foreach(glob($dir.'*.*') as $v){ unlink($v); } $dir2 = 'image/'; foreach(glob($dir2.'*.*') as $v){ unlink($v); } the first part deletes all the thumbnail images and the second all the original images. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.