Jump to content

remove a folder


westman

Recommended Posts

i whould like to know how to remove a user folder from my site.

<?php$target = "myFolder";if (is_dir($target)) {    rmdir($target); // Remove it now}?>

this code is graet but in some users folders they will be files and some will be emptyThe rmdir() filesystem function in PHP will remove or delete a directory, only if the folder is empty. so how do i delete a folder nomater what is in it?

Link to comment
Share on other sites

Check if the folder is empty, if not delete all of it's files and then delete the folder. I am not providing any code because i've never used the functions that are required before. I believe those are the functions you will have to use though. http://www.php.net/manual/en/function.scandir.phphttp://php.net/manual/en/function.unlink.php

Link to comment
Share on other sites

Here's a function to delete a directory:

function delete_directory($directory, $empty=false){  if(substr($directory,-1) == DIRECTORY_SEPARATOR)  {    $directory = substr($directory,0,-1);  }  if(!is_dir($directory) || !is_readable($directory))  {    return false;  }  else  {    $handle = opendir($directory);    while (false !== ($item = readdir($handle)))    {      if($item != '.' && $item != '..')      {        $path = $directory.DIRECTORY_SEPARATOR.$item;        if(is_dir($path))        {          delete_directory($path);        }        else        {          unlink($path);        }      }    }    closedir($handle);     if($empty == false)    {      if(!rmdir($directory))      {        return false;      }    }    return true;  }}

If you pass true as the second parameter then it will delete all files and subdirectories but leave the parent directory there.

Link to comment
Share on other sites

Thank you Member, I strive to write the flyest code. I would encourage you to look up each of those functions in the manual if you don't know what they do, but in general that is a recursive function (a function that calls itself). It checks if the directory name ends with a slash and removes that if so, then it checks if $directory is a readable directory and quits if it's not, then it gets a list of everything in the directory and loops through it. If each item is a directory then the function calls itself to delete that directory, or if it's a file then it deletes the file.

Link to comment
Share on other sites

You would include the code above to define the delete_directory function in your script, and then you can call the function like any other function and pass it the name of the directory you want to delete. There are some examples of creating and using functions here: http://www.w3schools.com/php/php_functions.asp

Link to comment
Share on other sites

ok so if i run this script...

<?php$directory = "../user/$user_id";function delete_directory($directory, $empty=false){  if(substr($directory,-1) == DIRECTORY_SEPARATOR){    $directory = substr($directory,0,-1);  }   if(!is_dir($directory) || !is_readable($directory)){    return false;  }   else{    $handle = opendir($directory);    while (false !== ($item = readdir($handle))){	  if($item != '.' && $item != '..'){	    $path = $directory.DIRECTORY_SEPARATOR.$item;	    if(is_dir($path)){		  delete_directory($path); }	     else{		  unlink($path);	    }	  }    }    closedir($handle);    if($empty == false){	  if(!rmdir($directory)){	    return false; }    }    return true; }}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-- remove user --/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////$sql_del = mysql_query("DELETE * FROM users WHERE id='$user_id' LIMIT 1");?>

have i defined the right directory to delete (have i use the right verible)?or in better words, have i deleted flie "../user/$user_id"?

Link to comment
Share on other sites

hmmm i see so i use function delete_directory(); to run the function aboveand it would look like...

<?php/////////// name the folder to be deleted/////////$directory = "../user/$user_id";//////////////////////////////////////////////////function delete_directory($directory, $empty=false){  if(substr($directory,-1) == DIRECTORY_SEPARATOR){	$directory = substr($directory,0,-1);  }   if(!is_dir($directory) || !is_readable($directory)){	return false;  }   else{	$handle = opendir($directory);	while (false !== ($item = readdir($handle))){		  if($item != '.' && $item != '..'){			$path = $directory.DIRECTORY_SEPARATOR.$item;			if(is_dir($path)){				  delete_directory($path); }		    else{				  unlink($path);			}		  }	}	closedir($handle); 	if($empty == false){		  if(!rmdir($directory)){			return false; }	}	return true; }}///////////// run the function /////////////////delete_directory();////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-- remove user --/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////$sql_del = mysql_query("DELETE * FROM users WHERE id='$user_id' LIMIT 1");?>

will that work?

Link to comment
Share on other sites

well, if you don't want it to do anything, then yes. But then you wouldn't be paying attention to JSG's example very well. the delete directory function expects at least one parameter, the directory to delete, with an optional second parameter. read what people are saying, and read the tutorials if you aren't sure.

You can use whatever variable name you want, or none at all, but you need to actually run the function. All you've done is define the function. delete_directory("../user/$user_id");
You would include the code above to define the delete_directory function in your script, and then you can call the function like any other function and pass it the name of the directory you want to delete. There are some examples of creating and using functions here: http://www.w3schools...p_functions.asp
or just try it.
Link to comment
Share on other sites

This is correct yes. You should really take a loot at the links provided about functions. You'll have to use them sooner or later.

Link to comment
Share on other sites

  • 3 weeks later...

Archived

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

×
×
  • Create New...