PDA

View Full Version : unlink()?


IKinsler
07-12-2002, 06:24 PM
How do you delete a directory?

I have always used unlink() to delete files, and just the other day tried to delete a directory with it. This is what I put:

unlink("./folder/");

Is there a reason that doesnt work? Do I need an extra parameter if it is for a directory?

Thanks in advance!

Feyd
07-12-2002, 07:38 PM
Try rmdir

More info : http://www.php.net/manual/en/function.rmdir.php

Flamerule
07-12-2002, 07:41 PM
Are you sure the dir dosn't have any files in it? If so it won't work : you need to delete them first.

IKinsler
07-12-2002, 07:47 PM
OH! Well that's my problem then! I've still got files! I will try deleting them and THEN rmdir().

Thanks to both of you!

Feyd
07-12-2002, 07:48 PM
Here's a function somebody came up with a while ago that will clean the files in the dir and them remove the dir...


<?php
function delete($file) {
chmod($file,0777);
if (is_dir($file)) {
$handle = opendir($file);
while($filename = readdir($handle)) {
if ($filename != "." && $filename != "..")
{
delete($file."/".$filename);
}
}
closedir($handle);
rmdir($file);
} else {
unlink($file);
}
}
delete("ordnername");
?>

IKinsler
07-12-2002, 09:28 PM
Thanks, Feyd!

I had that figured out, but unfortunately that doesn't work either! You see, I have no idea how many directories are in the directory I wish to delete - it could be 10 or 20! I had a function that would delete it if it was a file, and empty it and then delete it if it was a dir. But, apparently you cannot launch a function if you're already in it! :D So that didn't work either.

Flamerule
07-13-2002, 07:10 AM
How about having two functions :

One that does the cleaning
One that calls the one that does the cleaning.

The the first function goes upon another dir it calls the second function which calls the first.....

Should work.....:thumbsup:

firepages
07-13-2002, 10:04 AM
not for the feint-hearted...

exec('rm -f -R /get/this/path/right/directory');

but be sure to get the path right else tears will be forthcoming & dont blame me.
note some hosts disallow exec() calls.