?
Solved

file deleting

Posted on 2005-03-15
2
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 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