PDA

View Full Version : Delete Directorty


DsgnrsTLZAdmin
12-14-2004, 11:39 PM
How do I delete a directory that is not empty? If not possible, how do I delete all the file in a directory?

fci
12-15-2004, 12:16 AM
How do I delete a directory that is not empty? If not possible, how do I delete all the file in a directory?

if your server is running Linux:shell_exec('rm -rf /path/to/dir/');
or you could use php's functions to do it, but you would probably have to write a recursive function... the above is faster though so try that.

DsgnrsTLZAdmin
12-15-2004, 01:41 AM
Thank you. That works out. Look like a major potential security problem however.

fci
12-15-2004, 01:58 AM
Thank you. That works out. Look like a major potential security problem however.

yeah. you can do escapeshellarg, http://www.php.net/escapeshellarg
shell_exec('rm -rf '.escapeshellarg($path));

I probably should've mentioned that in my previous.. sorry about that.

Horus Kol
12-15-2004, 08:56 AM
I wrote this recursive function to strip all the contents from a given directory:


function file_RemoveDir($dir)
{
$current_dir = opendir($dir);

while ($f = readdir($current_dir))
{
if ((is_dir("$dir/$f"))&&($f != ".")&&($f != ".."))
file_RemoveDir($dir);
elseif (($f != ".")&&($f != ".."))
unlink("$dir/$f");
}

closedir($current_dir);
rmdir($dir);
}