Reply to Re: Select random files out of a directory

Your name:

Reply:


Posted by Curtis on 01/05/07 11:12

$dir = './';
$files = array();
$file = '';
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) $files[] = $file;
}
}
}

// specify an int for the optional 2nd arg if you
// need more than one random file
$randomKeys = array_rand($files);
$randomFile = $files[$randomKeys[0]];

$files holds an array of all files in the directory specified in the
$dir variable. This can take the place of scandir if you're running on
a version of PHP prior to 5.x.

On Jan 4, 9:30 pm, Hendri Kurniawan <ask...@email.com> wrote:
> BTW scandir is not available prior to PHP5
> Just a note
>
> Hendri Kurniawan
>
> Skeleton Man wrote:
> >> the problem is:
> >> there are too many files in the folder, e.g. over 10K or more, i don't
> >> want to list it
> >> but i know that it must contains 10K files, so it is possible to get ,
> >> say 1020th file under this folder?
>
> > Use scandir() to return an array of filenames and array_rand() to pick a
> > random entry from that list.. (see the manual for details)
>
> > Chris

[Back to original 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

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