Jump to content

Delete Certain Files from A Directory...


bizerk

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/50988-delete-certain-files-from-a-directory/
Share on other sites

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

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);
} 
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.