Get all the files within a directory

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
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Get all the files within a directory

Post by evilmonkey »

Hello,

I'm trying to create a script that will get all the pictures within a directory, and thorw them onto a page. The directory is local (as in on the same server where php is), and I can assume will be chmod'ed 777. The path will be known. Also, I will need to get the names of the pictures into some kind of array, so I can do a mass resize using GD. Something like:

Code: Select all

$pics = //array of picture names

for ($i=0; $i<=sizeof($pics)-1; $i++){
  resize($pics[$i]); //my own function
  save($pics[$i], "newname".$i.".jpg"); //my own function
  chmod ("newname".$i.".jpg", 0777)
}
Any help is appreciated. Thanks. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

glob() the folder, interate over each checking it first if it is_file() and getimagesize() if so to verify it is an image. Add it to the array. Repeat until you run out of entries in the return from glob().
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Check out this script, it'll do that.

http://webgeneius.com/demo/index.php
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would also like to point you to http://ca.php.net/manual/en/function.glob.php#37184 for scanning for mutliple extensions.

As mentioned in that post, you want to be careful because it may be case sensitive
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

glob() is cool!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Jcart wrote: As mentioned in that post, you want to be careful because it may be case sensitive
Adding to that, I found this in the php.net comments:
Note that, in some configurations, the search is case-sensitive! You'll need to have something like:

<?php
$images = glob("/path/to/images/{*.jpg,*.JPG}", GLOB_BRACE);
?>

Also on some servers, I have seen such scripts 'crash' with an CGI Error ("...not returning a complete set of HTTP headers...") when glob could not find any match!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Assuming PHP was installed as CGI binary.. which is not so common
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Oh, wow! This is awsome, you guys are the best! Thanks. A premade script won't do it btw because I'm integrating this into a website, but this will amke it much easier for me. Thanks very much! I'll keep the case-sensitive part in mind.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Evil Monkeys Signature wrote:Young people of Toronto: Prepare to be blown away!
I'm young and from Toronto.. and waiting to be blown away :lol:
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Jcart wrote:
Evil Monkeys Signature wrote:Young people of Toronto: Prepare to be blown away!
I'm young and from Toronto.. and waiting to be blown away :lol:
I can't say exactly what I'm working on, I can only say that I'm about 95% done (this has been in development for the last 1.5 years, and featured both technical and social development). It shoudl be up in a month tops.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

I have one more question about glob. Assuming I'm doing something like the following:

Code: Select all

<?php
foreach (glob("*.{JPG, jpg}",GLOB_BRACE) as $filename) {
   $pics[] = $filename;
}
?>
Is there a way to have the files sorted by last modified date in descending order (newer files first)? Thanks. :)
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Loop through them and sort them via filectime();
Post Reply