dsjoes Posted March 14, 2011 Share Posted March 14, 2011 how do i make this part of the script delete the folder and contents. <?php $path = "../../gallery/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { rmdir($path. "/" . $file) or die("Failed to delete file"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/ Share on other sites More sharing options...
RussellReal Posted March 14, 2011 Share Posted March 14, 2011 unlink? Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187308 Share on other sites More sharing options...
dsjoes Posted March 14, 2011 Author Share Posted March 14, 2011 unlink? yes but how do i add it Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187361 Share on other sites More sharing options...
dsjoes Posted March 14, 2011 Author Share Posted March 14, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187514 Share on other sites More sharing options...
dsjoes Posted March 15, 2011 Author Share Posted March 15, 2011 anyone Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187730 Share on other sites More sharing options...
vicodin Posted March 15, 2011 Share Posted March 15, 2011 You just did... If its not working then its a permission issue on your server. Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187748 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 You need to recursively delete the contents of the directory, before you can delete it. There's plenty of functions/snippets to search for through Google that should help you there. As vicodin suggested, you may run into permission issues as well. Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187753 Share on other sites More sharing options...
dsjoes Posted March 15, 2011 Author Share Posted March 15, 2011 i have tryed the below but i get 2 errors <?php $path = "../../gallery2.0/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { $mydir = $path.$file; $d = dir($mydir); while($entry = $d->read()) { if ($entry!= "." && $entry!= "..") { unlink($entry); } } $d->close(); rmdir($mydir); } } ?> these are the errors the folder it is trying to delete is folder1 and the contents is just a picture called pic1.jpg but the errors say the picture isn't there but it is because it is picking its name up and the seconds says the folder is not empty. Warning: unlink(pic1.jpg) [function.unlink]: No such file or directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 63 Warning: rmdir(../../gallery2.0/gallery_files/gallery/Folder1) [function.rmdir]: Directory not empty in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 67 Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187768 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 Clue is in the error: Warning: unlink(pic1.jpg) [function.unlink]: No such file or directory You're not specifying the path to the file before you try to unlink() it. Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187770 Share on other sites More sharing options...
dsjoes Posted March 15, 2011 Author Share Posted March 15, 2011 thanks fixed it unlink($mydir."/".$entry); there is a folder inside called thumbnails how do i do it so it deletes that aswell. forgot about it until now Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187780 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 You need to use a recursive function for this. Theoretically that sub-directory could contain a sub-directory, which contains a sub-directory, which contains a sub-directory, etc. You need to remove the contents of each before you can delete it. Rather than nesting loops within loops, just use a function that will repeatedly (recursively) call itself. I provided a Google link earlier that should help you. Quote Link to comment https://forums.phpfreaks.com/topic/230587-delete-folder-and-contents/#findComment-1187783 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.