Posted by d on 02/14/06 12:35
"Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message
news:kZGdnQVGF6sV42zeRVn-pg@comcast.com...
>
> "Andy Hassall" <andy@andyh.co.uk> wrote in message
> news:6ht1v1hcjoeo7p4n0o26erjpas270j0k1g@4ax.com...
>> On 13 Feb 2006 12:14:40 -0800, laredotornado@zipmail.com wrote:
>>
>>>Using PHP 4, what are the shortest amount of lines I can write to
>>>delete all the files in a given directory?
>>
> this works for one directory, but not subdirectories under it. for that,
> it needs to be recursive.
>
> function zapdir($dir) {
> if ($dir = opendir($dir)) {
> while (($file = readdir($dir)) !== false) {
> if (is_file("$dir/$file")) {
> unlink("$dir/$file");
> }
> //is a directory or a symlink
> if (is_dir("$dir/$file") && "." != $file && ".." != $file) {
> zapdir("$dir/$file");
> rmdir("$dir/$file") or echo "unable to remove directory
> $dir/$file\n";
> }
> if (is_link("$dir/$file")) {
> unlink("$dir/$file"); //remove symbolic link. does not
> remove original file AFAIK
> }
> }
> }
> }
> zapdir('/x/y/z');
That will run out of stack space in large directory trees... Best to close
the $dir before calling zapdir...
> If you really want it shorter you can remove some of the braces.
>>
>> --
>> Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
>> http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
>
>
[Back to original message]
|