dsjoes Posted March 18, 2011 Share Posted March 18, 2011 i have the below script but it doesn't work it just refreshes the page anyone know what is wrong <?php $path = "../../gallery/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { $dirname = $path; //Recursive Delete Function function DELETE_RECURSIVE_DIRS($dirname) { // recursive function to delete // all subdirectories and contents: if(is_dir($dirname))$dir_handle=opendir($dirname); while($file=readdir($dir_handle)) { if($file!="." && $file!="..") { if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); else DELETE_RECURSIVE_DIRS($dirname."/".$file); } } closedir($dir_handle); rmdir($dirname); return true; } } } ?> Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 18, 2011 Share Posted March 18, 2011 You are not actually calling your function. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189082 Share on other sites More sharing options...
dsjoes Posted March 18, 2011 Author Share Posted March 18, 2011 what do i need to do then? Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189100 Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2011 Share Posted March 18, 2011 you declare the function, but you never call it. http://www.tizag.com/phpT/phpfunctions.php Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189138 Share on other sites More sharing options...
dsjoes Posted March 18, 2011 Author Share Posted March 18, 2011 it has a separate form that i use to select a check box and then click delete that works it is just the delete part of the script. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189153 Share on other sites More sharing options...
dsjoes Posted March 19, 2011 Author Share Posted March 19, 2011 bump? Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189721 Share on other sites More sharing options...
creata.physics Posted March 19, 2011 Share Posted March 19, 2011 this would be better, looks like that function is only needed once, so you minus well take it out. <?php $path = "../../gallery/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { $dirname = $path; if(is_dir($dirname))$dir_handle=opendir($dirname); while($file=readdir($dir_handle)) { if($file!="." && $file!="..") { if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); else rmdir($dirname); } } } } ?> Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189727 Share on other sites More sharing options...
dsjoes Posted March 20, 2011 Author Share Posted March 20, 2011 i get this error now Warning: readdir(): supplied argument is not a valid Directory resource Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189742 Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 after this if(is_dir($dirname))$dir_handle=opendir($dirname); add ?><pre><?php print_r($dir_handle); ?></pre><?php and post what you get, thanks. The issue now, seems to be coming from $dir_handle not being read properly. We need to check if opendir was a success, so we need to dump $dir_handle. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189744 Share on other sites More sharing options...
dsjoes Posted March 20, 2011 Author Share Posted March 20, 2011 nothing has changed just the error is displayed Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189745 Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 That means opendir is not getting the path. We may have an issue with is_dir checking the path. What i'd remove $dirname = $path; and replace $dirname with path everywhere else. Try this one out. <?php $path = "../../gallery/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { if(is_dir($path)) { $dir_handle=opendir($path); } else { die('directory not found.'); } while($file=readdir($dir_handle)) { if($file!="." && $file!="..") { if(!is_dir($path."/".$file)) { // we unlink the file the remove the directory unlink ($path."/".$file); rmdir($path); } } } } ?> Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189748 Share on other sites More sharing options...
dsjoes Posted March 20, 2011 Author Share Posted March 20, 2011 directory not found. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189756 Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 I can't do everything for you as this is not my script. So if the directory is not found, what do you think you should do next? I personally think you should check $path and make sure it has the correct directory, i don't know your dir info, so I can't make this next part for you. It's up to you to set the proper directory and to get the script to move past that part, once you've done that and if you're still having issues feel free to reply. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189758 Share on other sites More sharing options...
dsjoes Posted March 20, 2011 Author Share Posted March 20, 2011 fixed it but now i get a load of unlink rmdir errors Warning: unlink(../../gallery/gallery_files/gallery/Group1/.) [function.unlink]: Is a directory Warning: rmdir(../../gallery/gallery_files/gallery/Group1) [function.rmdir]: Directory not empty Warning: unlink(../../gallery/gallery_files/gallery/Group1/..) [function.unlink]: Is a directory Warning: unlink(../../gallery/gallery_files/gallery/Group1/thumbnails) [function.unlink]: Is a directory Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189774 Share on other sites More sharing options...
creata.physics Posted March 20, 2011 Share Posted March 20, 2011 Let's just try making your first code work. I'm confused about the $file != . && $file != .., so we'll just make your initial code call the function properly and we can go from there. $path = "../../gallery/gallery_files/gallery/"; $dirname = $path; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { if (is_dir($dirname)) { DELETE_RECURSIVE_DIRS($dirname); } else { die(' dirname variable is wrong, fix the path.'); } } } function DELETE_RECURSIVE_DIRS($dirname) { // recursive function to delete // all subdirectories and contents: if(is_dir($dirname))$dir_handle=opendir($dirname); while($file=readdir($dir_handle)) { if($file!="." && $file!="..") { if(!is_dir($dirname."/".$file))unlink ($dirname."/".$file); else DELETE_RECURSIVE_DIRS($dirname."/".$file); } } closedir($dir_handle); rmdir($dirname); return true; } This is your function, unmodified, i've just modified other parts so it would be called properly, i added error handling to make sure that the directory name gets an error if not found. So we can go from here, try this and tell me what happens. Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1189781 Share on other sites More sharing options...
dsjoes Posted March 20, 2011 Author Share Posted March 20, 2011 thank you that works i had to change the part below because it deleted all folders and images instead of the selected folder. if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { $dirname = $path.$file; Link to comment https://forums.phpfreaks.com/topic/230997-recursive-delete-function/#findComment-1190053 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.