Re: array from folder
| From: | Richard Lynch | Date: | Mon, 25 Apr 2005 23:59:55 +0000 |
| Subject: | Re: array from folder | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-213846@lists.php.net to get a copy of this message | ||
On Mon, April 25, 2005 10:54 am, Jason Barnett said:
> Jay Blanchard wrote:
>> [snip]
>> Can PHP generate an array based on file names in a folder?
>>
>> For example, if I have a folder called "photos" that includes three
>> files -- tree.jpg, house.jpg and boat.jpg -- can PHP "look" at the file
>> and generate a variable $photos= array ("tree",
>> "house","boat"). Any
>> ideas for how to "sniff" out a folder to determine how many files are in
>> it and then create an array with the file names in it?
>> [/snip]
>>
>> Start with http://www.php.net/readdir
>
> Yep.
>
> <?php
>
> $dp = fopen('/path/to/dir', 'r');
This should be http://php.net/opendir not fopen. Though fopen
might work
on some platforms, I guess maybe...
> while(false !== $files[] = readdir($dp)) {}
This is a bit terse, and will include "." and ".." (with opendir) which
you probably don't want...
while ($file = readdir($dp)){
if ($file != '.' && $file != '..'){
$files[] = $file;
}
}
> closedir($dp);
> $str = implode('', $files);
> var_dump($str);
>
> ?>
>
--
Like Music?
http://l-i-e.com/artists.htm