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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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