Jump to content

Removing all the Files of a Certain Directory, and SPECIFIC files from a Directo


bizerk

Recommended Posts

Alright well here is what I have for the remove ALL FILES of a directory:

 

<?
//delete ALL FILES
$name = $_POST['name'];
$password = $_POST['pass'];
if($name == "admin" && $password == "admin")
{ 
  $dirname = "/files/";
   if (!is_dir($dirname))
       return false;
   $dscan = array(realpath($dirname));
   $darr = array();
   while (!empty($dscan)) {
       $dcur = array_pop($dscan);
       $darr[] = $dcur;
       if ($d=opendir($dcur)) {
           while ($f=readdir($d)) {
               if ($f=='.' || $f=='..')
                   continue;
               $f=$dcur.'/'.$f;
               if (is_dir($f))
                   $dscan[] = $f;
               else
                   unlink($f);
           }
           closedir($d);
       }
   }
   $i_until = ($only_empty)? 1 : 0;
   for ($i=count($darr)-1; $i>=$i_until; $i--) {
       echo "\nDeleting '".$darr[$i]."' ... ";
       if (rmdir($darr[$i]))
           echo "ok";
       else
           echo "FAIL";
   }
   return (($only_empty)? (count(scandir)<=2) : (!is_dir($dirname)));
}else{
echo "Do not have the correct Privlages";
}

?>

 

It does not make any errors, but it dosen't do anything either :/.

 

Here is what I used to delete specific files. Let's say for example ".txt" files.

 

<?
//delete all jpgs
$name = $_POST['name'];
$password = $_POST['pass'];
if($name == "admin" && $password == "admin")
{ 

foreach (glob("/files/*.txt") as $filename) {
   echo "$filename size " . filesize($filename) . "\n";
   unlink($filename);
}
}else{
echo "You do not have the Right Privlages to Delete any Files";

?>

 

but these both do not seem to be workingg :/. Any better suggestions or previosley written code so I can delete all files in a directory and SPECIFIC files like certain extensions? I could not get recursive deleting to work either...

 

does

 

  echo "$filename size " . filesize($filename) . "\n";

 

display the correct filename  if it does

 

unlink($filename);

 

deletes the filename in the current folder.. i think u need

 

unlink("\files\".$filename);

 

opps edit

 

unlink("/files/".$filename);

Okay thanks guys, I got everything to work here is what I ended up using for the delete ALL code:

 

<?
$name = $_POST['name'];
$password = $_POST['password'];
if($name == "admin" && $password == "examplepass") {
$dir = "files/";
$msg = "";
function deldir($dir) {
if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
   if ($file != "." && $file != "..") unlink($dir.$file);
  }
closedir($handle);
}
echo "Directory has been deleted";
}
$result =deldir($dir);

{
#create output message
if ($result == true)
{
$msg = "Done!";
}
}
}else{
echo "Not a Admin Account click <a href='index.php'>here</a> to go back";
}

?>

 

and the array_map... I am not quite understanding what you want me to do...

here is what i have right now for the txt.php file:

<?
//delete all txts
$name = $_POST['name2'];
$password = $_POST['pass2'];
if($name == "admin" && $password == "pass123"){ 
array_map ( 'unlink', glob ( './files/*.txt' ) );
}else{
echo "You do not have the Right Privlages to Delete any Files";
}

?>

 

is that supposed to work, because it is not working correctly...:/ any other suggestions?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.