YOMAMA Suspended 6218 Posts user info edit post |
I wrote this to read the current dir and retun all the file names if they are image files.
Right now it just takes the returned file names and prints them to a page with the img tag so you can see the images. What I am wondering is if there is a cleaner way to do so. Im not too PHP savy but I was wondering what the PHP experts think.
Thanks!
//Create list of Images $list = array(); if ($dir = opendir('./')) { while ($file = readdir($dir)) { //Finds image files if (eregi('^(.+\.(((j|J)(p|P)(g|G))|((g|G)(i|I)(f|F))|((p|P)(n|N)(g|G))))$', $file)) $list[] = $file; } closedir($dir); } asort($list); // sort alpha
foreach ($list as $pic) { echo "img src='$pic' br"; $i++; } ?>
[Edited on January 8, 2006 at 4:52 PM. Reason : img tag craps out the display. SO I know I need teh brackets around the html in the echo tag]1/8/2006 4:48:55 PM |
qntmfred retired 40817 Posts user info edit post |
if (eregi('^(.+\.((jpg))|(gif)|(png)))$', $file))
will work since you're using case insensitive eregi() and is much easier to read/maintain. otherwise, i'd say you're doing fine.
edit:
you could shorten it still to eregi('\.((jpg))|(gif)|(png))$', $file) - you don't need to match anything before the .jpg (unless you want the filename, which in your code you weren't using)
[Edited on January 8, 2006 at 5:31 PM. Reason : edit]1/8/2006 5:27:19 PM |
YOMAMA Suspended 6218 Posts user info edit post |
Hey thanks!
will do 1/8/2006 8:11:15 PM |
Stein All American 19842 Posts user info edit post |
preg_match("/\.(gif|jpg|png)$/i", $file);
Should be faster.
[Edited on January 8, 2006 at 9:40 PM. Reason : .] 1/8/2006 9:37:41 PM |
Noen All American 31346 Posts user info edit post |
yea preg functions are MUCH faster. 1/8/2006 10:01:50 PM |
YOMAMA Suspended 6218 Posts user info edit post |
yep thats what I ended up doing.
I was just waiting for the Noen comment. 1/8/2006 11:01:02 PM |
Stein All American 19842 Posts user info edit post |
Quote : | "I was just waiting for the Noen comment. " |
That hurts.1/9/2006 12:20:13 AM |
Quinn All American 16417 Posts user info edit post |
when you're in the know, you're Noen 1/9/2006 1:11:10 AM |
YOMAMA Suspended 6218 Posts user info edit post |
ohh thats bad! 1/9/2006 1:45:53 AM |