bizerk Posted May 11, 2007 Share Posted May 11, 2007 Is there any way to make a function that deletes all files of a certain EXTENSION? let's say ".txt" files... how could you use the unlink command to delete all files that have the extension ".txt"?? I know how to recursively unlink all files in a directory, but I can't seem to figure out how to delete all files with a certain extension... Quote Link to comment https://forums.phpfreaks.com/topic/50988-delete-certain-files-from-a-directory/ Share on other sites More sharing options...
kalivos Posted May 11, 2007 Share Posted May 11, 2007 I threw in a sample amount of file extensions. Feel free to change it up. Also, I would run it first before uncommenting the delete line. Just to make sure you are aware of what you are removing ;-) $some_dir = "my_img_dir/"; foreach (glob("$some_dir/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $file) { echo "Deleting ".$file." <br />"; //unset($file); } -Kalivos Quote Link to comment https://forums.phpfreaks.com/topic/50988-delete-certain-files-from-a-directory/#findComment-250885 Share on other sites More sharing options...
bizerk Posted May 11, 2007 Author Share Posted May 11, 2007 Hey alright, well it echoes everything and reads the directory fine, but it dosen't actually UNLINK the file. and yes i did take the unlink func out of it's comment... here's what i have: ? $name = $_POST['name2']; $pass = $_POST['pass2']; if($name == "admin" && $password == "fakepass") { $some_dir = "../pictures/"; foreach (glob("$some_dir/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG,*.JPEG,*.jpeg}", GLOB_BRACE) as $file) { echo "Deleting ".$file." <br />"; unset($file); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50988-delete-certain-files-from-a-directory/#findComment-250911 Share on other sites More sharing options...
kalivos Posted May 11, 2007 Share Posted May 11, 2007 ermm set the directory in unset unset($some_dir."/".$file); Quote Link to comment https://forums.phpfreaks.com/topic/50988-delete-certain-files-from-a-directory/#findComment-250936 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.