469,336 Members | 5,276 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Select random files out of a directory

for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.

Jan 5 '07 #1
10 9992
Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
Jan 5 '07 #2

Hendri Kurniawan gDG
Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
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?

Jan 5 '07 #3
>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
Jan 5 '07 #4
Unless other people knows any other methods.

for($i = 0; $i < 1024; $i++) $fileName = readdir($dirHandler);
print $fileName;

Now if you know the file names, that is another matter.

Hendri Kurniawan

howa wrote:
Hendri Kurniawan gDG
>Yes...
See opendir, readdir, rand
And get creative

Hendri Kurniawan
howa wrote:
>>for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.

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?
Jan 5 '07 #5
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

Jan 5 '07 #6
If you have access to system commands you could try the following:

1) Get the number of files using something like: ls | wc
2) Use your random generator
3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1

Have fun
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
Jan 5 '07 #7
$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.comwrote:
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
Jan 5 '07 #8

Martin Mandl - m2m tech support gDG
If you have access to system commands you could try the following:

1) Get the number of files using something like: ls | wc
2) Use your random generator
3) Now pick your line e.g. for file # 123: ls | head -123 | tail -1

Have fun
howa wrote:
for example, there are 10K files in the directory,

given that i don't know if files name, is it possible to fetch a file
randomly?

thanks.
thanks....all examples should work for me...but a little bit slow as my
folder contains more than 10K files....thanks anyway

Jan 5 '07 #9
Another idea: Why don't you load the names of your files into a
database. If you do this with an automatic task, it does not matter how
long it takes. ... and getting a random result form the database should
not take that long ...

howa wrote:
thanks....all examples should work for me...but a little bit slow as my
folder contains more than 10K files....thanks anyway
Jan 5 '07 #10
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Curtis schrieb:
$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]];

Without the memory overhead selecting one random file could in addition
look like this then:

$dir = './';
$file = '';
mt_srand(time());
$probability=mt_getrandmax()/10000;
if ( is_dir($dir) ) {
if ( $d = opendir($dir) ) {
while ( ($file = readdir($d)) !== false ) {
if ( !is_dir($file) ) {
if (mt_rand()<$probability) break;
}
}
}
}

I know it's far from perfect and it's not equally distributed but might
save some execution time if you accidently select the second file ;) as
well as you don't have the memory overhead and it takes at most as long
as the original script. Important here is the probability variable,
which should be adjusted with an estimated file count of the desired folder.

Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.1 (MingW32)

iD8DBQFFnnuRyeCLzp/JKjARAgImAJ0WdU9IjvrF9+vgYqRGQk3ewvjW/ACfcrAx
i7zjE6lnU58u6SsykMualQs=
=4QPJ
-----END PGP SIGNATURE-----
Jan 5 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by kingofkolt | last post: by
6 posts views Thread by Acacia | last post: by
4 posts views Thread by SoulSniper | last post: by
4 posts views Thread by brett | last post: by
1 post views Thread by Sahus Pilwal | last post: by
39 posts views Thread by Alan Isaac | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.