Page 1 of 1

Place All Filenames From Directory Into Array?

Posted: Tue Jun 10, 2008 5:41 am
by djwk
Hi there,

Here's the code I have started with:

Code: Select all

<?php
$dir = 'gallery/galleries/gallery1/';
$filecount = 0;
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(!is_dir($f)) $filecount++;
}
}
echo 'there are ',$filecount,' files in this folder';
?>
Ok so $filecount gives me a value of 29 (29 files in my gallery1 directory)
So now what I want to do with that is put all those 29 filenames into an array.
Let's say the files in the gallery1 directory are named like this: 123.jpg, 456.jpg, 789.jpg etc.

Let's call my array "jpgarray"

If I were to echo my array this is what I would want it to echo:

jpgarray(0) = "123.jpg"
jpgarray(1) = "456.jpg"
jpgarray(2) = "789.jpg"
etc.


Could anyone point me in the right direction or give me some code snippets for this?

Re: Place All Filenames From Directory Into Array?

Posted: Tue Jun 10, 2008 5:50 am
by VladSun

Re: Place All Filenames From Directory Into Array?

Posted: Tue Jun 10, 2008 9:08 am
by madan koshti
2 ways
1]
$dir = "/tmp";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$filesArr [] = $filename;
}
print_r($filesArr );

---------------------------------------------------
2]
$filesArr = scandir($dir);
print_r($filesArr );

Re: Place All Filenames From Directory Into Array?

Posted: Tue Jun 10, 2008 11:16 am
by anti.veeranna
or the SPL way:

Code: Select all

<?php
$filenames = array();
 
foreach(new DirectoryIterator('.') as $Item)
{
    if ($Item->isFile()) {
        array_push($filenames, $Item->getFilename());
    }
}
 
print_r($filenames);
Alternatively, if you ever need to get files from all subdirectories as well, then you can do:

Code: Select all

$filenames = array();
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $Item)
{
    if ($Item->isFile()) {
        array_push($filenames, $Item->getFilename());
    }
}
 
print_r($filenames);

Re: Place All Filenames From Directory Into Array?

Posted: Fri Jun 13, 2008 3:46 am
by djwk
Hi anti.veeranna, I tried your first bit of code and here's the error I got:
Fatal error: Cannot instantiate non-existent class: directoryiterator

Any Ideas?

Re: Place All Filenames From Directory Into Array?

Posted: Fri Jun 13, 2008 4:02 am
by lonelywolf
djwk wrote:Hi anti.veeranna, I tried your first bit of code and here's the error I got:
Fatal error: Cannot instantiate non-existent class: directoryiterator
You need PHP version 5.x to use SPL (standard php library). It's a convinient way!

Re: Place All Filenames From Directory Into Array?

Posted: Fri Jun 13, 2008 5:42 am
by djwk
Sorry madan, I didn't even try your first piece of code, it worked a charm. Cheers buddy :)

Re: Place All Filenames From Directory Into Array?

Posted: Fri Jun 13, 2008 1:31 pm
by RobertGonzalez
It was mentioned before. glob(). Use it.

Re: Place All Filenames From Directory Into Array?

Posted: Sun Jun 15, 2008 1:15 pm
by anti.veeranna
Yep, my code needs PHP5, which has been out for 4 years already. You should use it :)