?
Solved

file deleting

Posted on 2005-03-15
2
Medium Priority
?
190 Views
Last Modified: 2006-11-17
I need a php script which will list all files on my website and delete them, with the exception of itself obviously. The reason for this is my site has been hacked and I needed to delete the entire site to take it offline.I did not have access to the internet except via my phone.
I intend to make it so as I can access it on my phone, enter a security pin which then deletes everything. . The wap side is no problem, I have deleted individual files but want to do all, incase new hack ones have been uploaded.
0
Comment
Question by:jdav3579
  • 2
2 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13550007
To read the contents of a directory:

<?php
$dir = "/mydir";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           if (is_file($file))
               echo "filename: $file : filetype: " . filetype($dir . $file) . "<br />\n";
       }
       closedir($dh);
   }
}
?>

To delete the contents of a directory, except one file:

<?php
$dir = "/mydir";
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           if (basename($file) != 'myscript.php'  and is_file($file)) //don't delete this file
               unlink($file);
       }
       closedir($dh);
   }
}
?>

You must of course have write permission for the files you want to delete. alo note that this script is not recursive - it will not delete folders, only files.

More info and examples here:

http://www.php.net/manual/en/function.opendir.php
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 2000 total points
ID: 13550022
oops, this line should be in a different order:

           if (is_file($file) and basename($file) != 'myscript.php') //don't delete this file
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question