Auto delete certain file types with php.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rushi
Forum Newbie
Posts: 2
Joined: Sat Jun 13, 2009 10:04 pm

Auto delete certain file types with php.

Post by Rushi »

I apologize in advance if I am asking on the wrong forum, or if I am asking a stupid question. Google turned up nothing, and this seemed to be the correct subforum.

What I want to create is a script that deletes certain types of files (.php and .php.mfh) in the same folder when run.
For example: A folder would contain autodel.php, x.php, x.php.mfh, and x.mfh. When run, autodel.php would delete all files with the suffixes .php, and .php.mfh. Thus, the script would delete x.php and x.php.mfh, while keeping itself intact, as well as other types of files.

I don't know how to achieve this, or if this is even possible. I would appreciate any help someone would have to offer.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Auto delete certain file types with php.

Post by requinix »

1. Make a list of file name patterns that you want to delete, like array("*.php", "x.*")
2. Loop through all the files in the current directory.
3. For each one, loop through the list of patterns. If the file matches one, is a file, and it isn't the current script, delete it and move on to the next file.
Rushi
Forum Newbie
Posts: 2
Joined: Sat Jun 13, 2009 10:04 pm

Re: Auto delete certain file types with php.

Post by Rushi »

tasairis wrote:1. Make a list of file name patterns that you want to delete, like array("*.php", "x.*")
2. Loop through all the files in the current directory.
3. For each one, loop through the list of patterns. If the file matches one, is a file, and it isn't the current script, delete it and move on to the next file.
Thanks, I have it working. One more question, though.
Here is my code.

Code: Select all

<?php
$scriptfile = "d.php";
 
if ($handle = opendir('.'))
{
    while (false !== ($file = readdir($handle))) 
    {
     if (fnmatch("*.php", $file))
     {
      if (is_file($file))
      {
       if ($file != $scriptfile)
       {
       echo ("$file is a php file, and is not the script.");
       unlink($file);
       echo (" File Deleted");
       echo ("<br />");
       }
      }
     }
     if (fnmatch("*.php.mfh", $file))
     {
      if (is_file($file))
      {
       if ($file != $scriptfile)
       {
       echo ("$file is a php file, and is not the script.");
       unlink($file);
       echo (" File Deleted");
       echo ("<br />");
       }
      }
     }
     if (fnmatch("*.html", $file))
     {
      if (is_file($file))
      {
       if ($file != $scriptfile)
       {
       echo ("$file is a php file, and is not the script.");
       unlink($file);
       echo (" File Deleted");
       echo ("<br />");
       }
      }
     }
    }
    closedir($handle);
}
?>
Is there an easier way to check if $file is the script?
Post Reply