jamiet757 Posted January 28, 2010 Share Posted January 28, 2010 I have a script that I want to delete all files in a folder (taken from a database) except for one named thumb1.jpg I have this, but this deletes all the files and a subfolder if there is one. How can I modify it to leave the folder and thumb1.jpg intact, but remove any other files in this folder? if($folder!="" and file_exists($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder)) { $dir = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder); while ($file = readdir ($dir)) { if($file <> "." && $file <> "..") { if(is_dir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file)) { $dir2 = opendir ($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); while ($file2 = readdir ($dir2)) { if($file2 <> "." && $file2 <> "..") { @unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file."/".$file2); } } @rmdir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); } else { @unlink($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder."/".$file); } } } @rmdir($_SERVER["DOCUMENT_ROOT"].site_root.site_upload_directory."/".$folder); } } Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/ Share on other sites More sharing options...
GrooN Posted January 28, 2010 Share Posted January 28, 2010 I think that can be done much faster <?php $files = glob("*"); foreach ($files as $file) { if ($file != "index.php") unlink($file); } ?> Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003310 Share on other sites More sharing options...
jamiet757 Posted January 28, 2010 Author Share Posted January 28, 2010 That seems good, so in your example, it will delete any file that is not named "index.php", and it will leave the folder intact? The only problem is, the folders are different, they are taken from the database depending on which entry I am deleting. How can I incorporate that into your example? Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003311 Share on other sites More sharing options...
GrooN Posted January 28, 2010 Share Posted January 28, 2010 Well no, actually that script deletes the folders too. But you can make the script more advanced, and check if the file is written like a file name like this: if (!preg_match("/^[^.].*[^.]/$", $file)) // If file doesn't match pattern skip. continue; Note: This wont work if the folder is named something like "my.folder" since this is name based. Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003314 Share on other sites More sharing options...
GrooN Posted January 28, 2010 Share Posted January 28, 2010 sorry, wrong regexp pattern use this: "/^([^\.\\]+*\.)+[^\.\\]+$/" Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003320 Share on other sites More sharing options...
jamiet757 Posted January 29, 2010 Author Share Posted January 29, 2010 Alright, but how can I implement that so that it does not delete a file named thumb1.jpg, I am not very good with understanding regex. Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003451 Share on other sites More sharing options...
GrooN Posted January 29, 2010 Share Posted January 29, 2010 Try this if ($file != "index.php" && $file != "thumb1.jpg") Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003584 Share on other sites More sharing options...
jamiet757 Posted January 29, 2010 Author Share Posted January 29, 2010 That worked great, thanks! Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003892 Share on other sites More sharing options...
GrooN Posted January 29, 2010 Share Posted January 29, 2010 Anytime dude ^^ Link to comment https://forums.phpfreaks.com/topic/190161-delete-all-files-in-folder-except-with-certain-name/#findComment-1003894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.