Adrienk Posted July 27, 2012 Share Posted July 27, 2012 So I am having issues. like mental issues over this.... I found a piece of code on the php manual site in relation to recursively deleting directories and modified it to this: function delete_contents_in_folder($path_to_dir){ if(is_file($path_to_dir)){ return @unlink($path_to_dir); } elseif(is_dir($path_to_dir)){ $scan = glob(rtrim($path_to_dir,'/').'/*'); foreach($scan as $index=>$path){ $this->delete_contents_in_folder($path); } if($path_to_dir == AISISCORE){ return; }else{ return @rmdir($path_to_dir); } } } essentially AISISCORE is the directory i want it to IGNORE. so essentially it deletes every file, sub directory and so on in AISISCORE but does not delete the root directory, in the end AISISCORE is left as an empty folder. What's the issue? I want to tell it to delete everything in AISISCORE as it already does BUT ignore specific files when doing so, that's the part I can't figure out how to do. Your help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/ Share on other sites More sharing options...
Adrienk Posted July 27, 2012 Author Share Posted July 27, 2012 So I assume no? I have noticed this form moves fast - hence the bump. sorry for rule breaking. kinda need an answer - dont hate me. Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364898 Share on other sites More sharing options...
Mahngiel Posted July 27, 2012 Share Posted July 27, 2012 * create an array of directories you do NOT want to delete * generate an array of directories in the folder * iterate through the existing directories * conditionally delete if not in your safe directory array Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364902 Share on other sites More sharing options...
NomadicJosh Posted July 27, 2012 Share Posted July 27, 2012 I worked on something similar for an application. My code was first to delete the files in the directory and then delete the directory. But I changed it around so you can see what how you can just delete the files but leave the directory alone: if( file_exists( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] ) ) { $files = glob( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] . '/*' ); foreach( $files as $file ) unlink($file); } I would probably do something like this with your code: function delete_contents_in_folder($path_to_dir){ if(file_exists($path_to_dir) && ($path_to_dir != 'AISISCORE')) { $files = glob( $path_to_dir . '/*' ); foreach( $files as $file ) unlink($file); rmdir( $path_to_dir ); } else { $files = glob( $path_to_dir . '/*' ); foreach( $files as $file ) unlink($file); } } I haven't tested it and not 100% sure if it will work, but you can test it out on a test directory first to make sure. Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364903 Share on other sites More sharing options...
Adrienk Posted July 27, 2012 Author Share Posted July 27, 2012 I worked on something similar for an application. My code was first to delete the files in the directory and then delete the directory. But I changed it around so you can see what how you can just delete the files but leave the directory alone: if( file_exists( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] ) ) { $files = glob( PM_DIR . 'projects/uploads/' . $_REQUEST['p_id'] . '/*' ); foreach( $files as $file ) unlink($file); } I would probably do something like this with your code: function delete_contents_in_folder($path_to_dir){ if(file_exists($path_to_dir) && ($path_to_dir != 'AISISCORE')) { $files = glob( $path_to_dir . '/*' ); foreach( $files as $file ) unlink($file); rmdir( $path_to_dir ); } else { $files = glob( $path_to_dir . '/*' ); foreach( $files as $file ) unlink($file); } } I haven't tested it and not 100% sure if it will work, but you can test it out on a test directory first to make sure. This, from my understanding - correct me if I am wrong. sais: delete all files except the directory I'm deleting files in. Which is fine. how ever I need to delete a;ll files in said directory EXCEPT for specified files. So essentially: AISISCORE would be empty - EXCEPT for the files I don't want deleted. maybe I missed something in your example? thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364907 Share on other sites More sharing options...
Mahngiel Posted July 27, 2012 Share Posted July 27, 2012 * create an array of directories you do NOT want to delete * generate an array of directories in the folder * iterate through the existing directories * conditionally delete if not in your safe directory array <?php $files_i_like = array( 'horses.php', 'dogs.php', 'pr0n.jpg'); //while in your loop if( !in_array( $file, $files_i_like) ) unlink( $file ); Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364909 Share on other sites More sharing options...
Adrienk Posted July 27, 2012 Author Share Posted July 27, 2012 Why thank you. Your help is much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364910 Share on other sites More sharing options...
NomadicJosh Posted July 27, 2012 Share Posted July 27, 2012 Ah, I see. I missed that part. As @Mahngiel has mentioned, you will need to correct an array of files you do not want deleted. Quote Link to comment https://forums.phpfreaks.com/topic/266343-how-to-recursively-delete-files-with-out-dealting-specific-files/#findComment-1364917 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.