Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Removing Unwanted Files

Depending on the FTP software a person is using, it is possible that there may be a file on the server that the software can not delete.

Of course, the first thing to do is verify that your server has not been hacked. Files that resist deletion have been a common aspect with most server hacks I have cleaned up. The file name may appear familiar but with special characters introduced that the FTP software doesn't know how to handle.

This is not an article about server hacks. Yet, the above needed to be said as something to be on the lookout for.

Inadvertently prepending or appending a space to a file name can make a file non-deletable with certain FTP software. A control code within the file name can also play havoc. (One way control codes can be introduced is copying text from Word, OpenOffice, or other text-formatting software and then pasting it in as the file name.)

Things can be done inadvertently that make a file non-deletable.

As an example, I once uploaded a file and was unable to move it or rename it. I don't know what I did. Really. No idea. A PHP script showed me that the file got fully uploaded, but something went awry with the file name.

It took a PHP script to read the file name and then delete exactly what it read to finally get rid of the file. Then I uploaded the same file from my hard drive again and everything worked fine.

This article comes with a simple-to-use PHP RemoveUnwanted.php script to remove files that won't budge with regular FTP software.

You might never run into a situation where your FTP software won't delete a certain file from your server. But if you do so, RemoveUnwanted.php may come in quite handy.

Screenshot

I am unwilling to put a live example into this article because I don't want server files deleted by people testing the example. Instead, I made a screenshot.

The screenshot image is of the RemoveUnwanted.php dashboard. (If the column width or your screen is narrower than the image, the image will be shrunk to fit.)

screenshot of RemoveUnwanted.jpg control panel

As the screenshot shows, it has a simple dashboard. RemoveUnwanted.php is intentionally unlisted to prevent inadvertent deletion.

To delete a file that FTP software will not:

  1. Upload RemoveUnwanted.php into the directory with the unwanted file. RemoveUnwanted.php is good to go as is, no customization required.

  2. Load the script by typing its URL into your browser.

  3. Select the file or files you want to delete.

  4. Click the Delete Checked Files button.

After using the RemoveUnwanted.php script, remove the RemoveUnwanted.php file from the server so others can't use it to delete files you don't want deleted.

Now the good part, the source code to RemoveUnwanted.php so you can use it on your domains.

The Source Code

This source code needs no customization. Copy it and save it as RemoveUnwanted.php (or other *.php file name).

<?php
/*
Remove Unwanted Files in This Directory
Version 1.0
May 24, 2022
Will Bontrager Software LLC
https://www.willmaster.com/
*/
$thisFileName = preg_replace('!^.*/!','',$_SERVER['PHP_SELF']);
if( count($_POST) )
{
   foreach( $_POST as $k => $v )
   {
      if( preg_match('/^delete_/',$k) ) { unlink($v); }
   }
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove Unwanted Files in This Directory</title>
<style type="text/css">
* { box-sizing:border-box; }
html, body { font-size:100%; font-family:sans-serif; }
table { border:1px solid black; border-collapse:collapse; margin:0 auto; }
th { font-size:90%; vertical-align:bottom; }
td { vertical-align:top; }
a { text-decoration:none; }
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data" accept-charset="utf-8" action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF'])); ?>">
<div style="display:table; margin:0 auto;">
<a href="https://www.willmaster.com/"><img src="https://www.willmaster.com/images/wmlogo_icon.gif" alt="logo"></a><h3 style="margin:0;">Remove Unwanted Files in This&nbsp;Directory:</h3>
<div style="clear:left;"></div>
</div>
<p style="margin-top:0; margin-bottom:1em; text-align:center;">
<?php echo(__DIR__); ?>
</p>
<table border="1" cellpadding="6" cellspacing="0">
<tr>
<th>File Name</th>
<th>Check<br>to<br>Delete</th>
</tr>
<?php
$count = 0;
foreach( glob('*') as $f )
{
   if( $f == $thisFileName ) { continue; }
   if( ! is_file($f) ) { continue; }
   $count++;
   $fhtml = htmlspecialchars($f);
   echo <<<LINE
<tr>
<td>$count: $fhtml</td>
<td style="text-align:center;"><input type="checkbox" name="delete_$count" value="$fhtml"></td>
</tr>
LINE;
}
?>
<tr>
<td colspan="2" style="text-align:right;">
<input type="submit" value="Delete Checked Files" name="button_catcher">
</td>
</tr>
</table>
<p style="display:table; margin:1em auto .5in auto;">
Copyright 2022 <a href="https://www.willmaster.com/">Will Bontrager Software LLC</a>
</p>
</form>
</body>
</html>

The above is the source code for the complete software. As noted earlier, no customization is required.

Keep it handy for when, or if, you stumble into a situation where your FTP software won't delete a file.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC