You are here: Re: array from folder « PHP « IT news, forums, messages
Re: array from folder

Posted by Matthew Weier O'Phinney on 10/04/49 11:14

* Ed Dorsch <edorsch@uoregon.edu>:
> 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?

You might want to try PEAR's File_Find package. With it, you could pass
a pattern to match, and receive an array of files that match. You could
then use array_map() to strip the suffix:

include_once 'File/Find.php';

function photo_basename($file)
{
list($name, $sfx) = explode('.', basename($file), 2);
return $name;
}

$photos = &File_Find::glob('/.*\.(jpg|png|gif)$/i', 'photos', 'perl');
$photos = array_map('photo_basename', $photos);

To determine the number of files, simply do a count() on the $photos
array.

--
Matthew Weier O'Phinney | WEBSITES:
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org
mailto:matthew@garden.org | http://vermontbotanical.org

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация