Welcome to WebmasterWorld Guest from 50.16.79.246

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Delete Entire Folder Contents With PHP

     
12:25 pm on Mar 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


What is the best way of deleting an entire folders contents in php

I looked at this, but not sure exactly how to implement.

[uk.php.net...]

I dont want to get involved with something that could mash my system if I get it wrong.

Cheers

12:33 pm on Mar 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


unlink("/path/to/folder/","*.php");

Will that work? :)

1:28 pm on Mar 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 9, 2003
posts:180
votes: 0


The function you need is rmdir() [uk.php.net]
6:41 pm on Mar 18, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12547
votes: 2


rmdir will not remove the directory if the directory is not empty and you will receive a warning message. You would either have to loop through and unlink any existing files, or you could invoke a system command on the directory.
6:42 pm on Mar 19, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


you could loop it like this too

<? 
$mydir = "/path/to/dir/";
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= "." && $entry!= "..") {
unlink($entry);
}
}
$d->close();
rmdir($mydir);
?>