Page 1 of 1

Auto delete certain file types with php.

Posted: Sat Jun 13, 2009 10:15 pm
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.

Re: Auto delete certain file types with php.

Posted: Sat Jun 13, 2009 11:21 pm
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.

Re: Auto delete certain file types with php.

Posted: Sun Jun 14, 2009 2:11 am
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?