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.
Auto delete certain file types with php.
Moderator: General Moderators
Re: Auto delete certain file types with php.
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.
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.
Thanks, I have it working. One more question, though.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.
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);
}
?>