0

how do i i figured out how to make one now i need to know how to delete the directory(and everythin thats inside it) WITH PHP

4
Contributors
13
Replies
14
Views
7 Years
Discussion Span
Last Post by SKANK!!!!!
0

This should work, but I have not tested it, just call deleteDir with the directory you want to remove.

<? 
function deleteDir($dir) { 
  if(is_dir($dir)) { 
    $dir_handle = opendir($dir);
    while($entry = readdir($dir)) { 
      if ($entry!= "." && $entry!= "..") { 
        unlink($entry); 
      } 
    }
  } 
  closedir($dir); 
  rmdir($dir);
}
?>

Edited by Will Gresham: n/a

0

This should work, but I have not tested it, just call deleteDir with the directory you want to remove.

<? 
function deleteDir($dir) { 
  if(is_dir($dir)) { 
    $dir_handle = opendir($dir);
    while($entry = readdir($dir)) { 
      if ($entry!= "." && $entry!= "..") { 
        unlink($entry); 
      } 
    }
  } 
  closedir($dir); 
  rmdir($dir);
}
?>

do you know how to delete the files in it too because i tried deleteing a directory with files in it and it wont delete. i need to delete ll the files and the directory

0

the function he gave you will cos of the unlink() this will delete the files in the folder before deleting the folder. the rmdir() only works on an empty folder and also try to get a php manual that will help you with php functions. shalom shalom

0

If it doesn't it's simple to delete the contents of a folder:

<?php
     $folder = "/FOLDER_NAME/";
     while( $file = scandir( $folder ) ) {
          rename( $file , false );
     }
?>
0

for some reason. something i did with the code caused it to delete the entire folder it was in i dont understand what i did. i had to get my backup cause it deleted my entire site!

if(isset($_POST[submit])){

$documentroot = $_SERVER['DOCUMENT_ROOT'];
$dir = $documentroot . '/' . $del_id;
function rmdir_r ( $dir, $DeleteMe = TRUE )
{
	if ( ! $dh = @opendir ( $dir ) ) return;
	while ( false !== ( $obj = readdir ( $dh ) ) )
	{
		if ( $obj == '.' || $obj == '..') continue;
		if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
	}
	
	closedir ( $dh );
	if ( $DeleteMe )
	{
		@rmdir ( $dir );
	}
}

$checkbox = $_POST['checkbox'];
$countCheck = count($_POST['checkbox']);
if($countCheck != "0"){
for($i=0;$i<$countCheck;$i++)
            {$del_id  = $checkbox[$i];
rmdir_r($dir);}}
 }

im thinking maybe because del_id was empty for some reaosn but i dont understand why.. is it because it was empty the reason it deleted my entire site? or what? cause cant i just check if it is empty like if $del_id != ""){go though with the driectory delete or was it something else? cause i dont understand

Edited by SKANK!!!!!: n/a

0

check the directory you pass to it, cos it will only delete the directory passed to it so error might be in your path

0

This line: $dir = $documentroot . '/' . $del_id; is before you set the value of $del_id.

Try this:

if($countCheck != "0") {
  for($i=0;$i<$countCheck;$i++) {
    $dir = $documentroot . '/' . $checkbox[$i];
    rmdir_r($dir);
  }
}

Also, try to keep code structure constant.. You have opening and closing braces all over the place and no apparent consistent indents, code is much more readable if you keep things consistent. :)

Edited by Will Gresham: n/a

0

This line: $dir = $documentroot . '/' . $del_id; is before you set the value of $del_id.

Try this:

if($countCheck != "0") {
  for($i=0;$i<$countCheck;$i++) {
    $dir = $documentroot . '/' . $checkbox[$i];
    rmdir_r($dir);
  }
}

Also, try to keep code structure constant.. You have opening and closing braces all over the place and no apparent consistent indents, code is much more readable if you keep things consistent. :)

i dont think checkbox will fit.
the value of the check box is the persons username or its supposed to be anyways i dont know how it became nothing or empty

0

OK SORRY I DIDNT UNDERSTAND AT FIRST it works now and doesnt delete the whole site, just the directorys and file contents. but what if accidentally the value of $checkbox[$i] was empty.? then it deletes my whole site again.
what do i do to make sure it doesnt do it if its empty because i tried if($checkbox[$i] != ""){do the code thing to delete directory;}

but it STILL DELETES ! when the variable was empty i dont udnerstand how to make it not do the code.

0

Try if( strlen( $checkbox ) > 1 ) { EXEC CODE... } as sometimes you can post a single whitespace wich can throw PHP off. Asuming your folders are more than 1 char long that is.

Edited by Froger93: n/a

0
if(isset($_POST[submit])){

$documentroot = $_SERVER['DOCUMENT_ROOT'];
function rmdir_r ( $dir, $DeleteMe = TRUE )
{
	if ( ! $dh = @opendir ( $dir ) ) return;
	while ( false !== ( $obj = readdir ( $dh ) ) )
	{
		if ( $obj == '.' || $obj == '..') continue;
		if ( ! @unlink ( $dir . '/' . $obj ) ) rmdir_r ( $dir . '/' . $obj, true );
	}
	
	closedir ( $dh );
	if ( $DeleteMe )
	{
		@rmdir ( $dir );
	}
}

$checkbox = $_POST['checkbox'];
$countCheck = count($_POST['checkbox']);
if($countCheck != "0"){
for($i=0;$i<$countCheck;$i++)
            {$del_id  = $checkbox[$i];

$checkitsauser = mysql_query("SELECT id FROM users WHERE username = '$del_id'");
$checkitsauser2 = mysql_num_rows($checkitsauser);
if($checkitsauser2 == 1){$theuserid = mysql_result($checkitsauser, 0);}
else{exit("theres no user by: $del_id  in the database");}
$dir = $documentroot . '/' . $del_id;
rmdir_r($dir);

}}
else{echo "you didnt check any box";}
 }

DOES THIS WORK? or is your way better? i dont understand if the one i have is ok or not. cause i really dont know why it deleted the whole site. i dont know if this is the proper precaution to take. if this isnt ok then the one u put with the strlen() where does that go?

Edited by SKANK!!!!!: n/a

0

After you have called the rmdir_r function you need to wrap the code that follows in the statement I gave you.

0

After you have called the rmdir_r function you need to wrap the code that follows in the statement I gave you.

if( strlen( $checkbox ) > 1 ) {
rmdir_r($dir);}

???

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.