Page 1 of 1

Get all the files within a directory

Posted: Tue Feb 28, 2006 6:01 pm
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. :)

Posted: Tue Feb 28, 2006 6:05 pm
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().

Posted: Tue Feb 28, 2006 6:07 pm
by neophyte
Check out this script, it'll do that.

http://webgeneius.com/demo/index.php

Posted: Tue Feb 28, 2006 6:08 pm
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

Posted: Tue Feb 28, 2006 6:09 pm
by neophyte
glob() is cool!

Posted: Tue Feb 28, 2006 6:13 pm
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!

Posted: Tue Feb 28, 2006 6:15 pm
by John Cartwright
Assuming PHP was installed as CGI binary.. which is not so common

Posted: Tue Feb 28, 2006 6:15 pm
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.

Posted: Tue Feb 28, 2006 6:22 pm
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:

Posted: Tue Feb 28, 2006 6:28 pm
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.

Posted: Thu Mar 02, 2006 4:46 pm
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. :)

Posted: Thu Mar 02, 2006 4:49 pm
by neophyte
Loop through them and sort them via filectime();