Place All Filenames From Directory Into Array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Place All Filenames From Directory Into Array?

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Place All Filenames From Directory Into Array?

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
madan koshti
Forum Commoner
Posts: 50
Joined: Fri Jun 06, 2008 4:25 am

Re: Place All Filenames From Directory Into Array?

Post 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 );
anti.veeranna
Forum Newbie
Posts: 4
Joined: Tue Jun 10, 2008 11:14 am
Location: Estonia

Re: Place All Filenames From Directory Into Array?

Post 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);
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Re: Place All Filenames From Directory Into Array?

Post 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?
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: Place All Filenames From Directory Into Array?

Post 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!
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Re: Place All Filenames From Directory Into Array?

Post by djwk »

Sorry madan, I didn't even try your first piece of code, it worked a charm. Cheers buddy :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Place All Filenames From Directory Into Array?

Post by RobertGonzalez »

It was mentioned before. glob(). Use it.
anti.veeranna
Forum Newbie
Posts: 4
Joined: Tue Jun 10, 2008 11:14 am
Location: Estonia

Re: Place All Filenames From Directory Into Array?

Post by anti.veeranna »

Yep, my code needs PHP5, which has been out for 4 years already. You should use it :)
Post Reply