Log in

View Full Version : Easy php help



jzhang1013
04-16-2009, 05:48 PM
Hey all so what I want to do is basically write a php code that pulls all my images from a certain folder filled with images and display them. Can someone write me a simple code that does that?

Thanks,
John

Spinethetic
04-16-2009, 07:10 PM
<?php
$files = glob("images/*.*");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'."&nbsp;&nbsp;";
}
?>

jzhang1013
04-16-2009, 08:39 PM
hey thanks! now where in that code would I specify the folder name? or does this code have to be put in the same folder as the images its grabbing?

Spinethetic
04-16-2009, 09:43 PM
hey thanks! now where in that code would I specify the folder name? or does this code have to be put in the same folder as the images its grabbing?

In the second line

$files = glob("images/*.*");
I am asuming your the image folder is titled "images", therefore if it is really titled "gobbeldygoop" the second line would be as so:

$files = glob("gobbeldygoop/*.*");

Best Regards
~Ross :)

jzhang1013
04-16-2009, 09:45 PM
ok so it supports the basics such as .jpg and .png? and itll just display how i style it correct? Thanks so far Ross!

Spinethetic
04-16-2009, 09:57 PM
It will display all format images compatable with the browsers. ie: I'am not sure but adobe illiustrator images can be displayed on a webpage when using Firefox but I'am not sure.

But generally speaking to cover 98% of web browsers, this script will display .JPG, .GIF, .PNG and .BMP files

EDIT: I just tested the script and discovered that it will only display images DIRECTLY in the folder specified by the script. It will not display any images in subfolders.

jzhang1013
04-16-2009, 10:06 PM
so i tried it and im getting this error..

Parse error: syntax error, unexpected ';' in /home/jzhang/public_html/testgrab/testgrab.php on line 3

Spinethetic
04-16-2009, 10:49 PM
Make sure you have copied it exactly as I have written, the absolute only thing that you need to change is the foldername betwen the quotation markes on line two. An error msg like that usually indicates a typo from either typing it in by hand too fast or maybe you messed something up when you altered the folder name in line 2

jzhang1013
04-16-2009, 11:00 PM
do i have to rename my images to 1, 2, 3, etc?

Schmoopy
04-16-2009, 11:10 PM
No, this will just go through all the files that the glob() function finds by using count(). It just uses $i as an index so it can go through a loop looking for each file.

You can name the files whatever you want.

If you wanted to make it more specific, for example if you have some files in the images folder that aren't images then you could put, for example:
glob("images/*.jpg");

jzhang1013
04-16-2009, 11:33 PM
ok finally got it to work. now if I want to input a preloader on this page what suggestions would you guys have? Should I start a new thread in the js section?

http://1013mm.com/testgrab.php

jzhang1013
04-16-2009, 11:35 PM
oh and I'm trying to use this preloader but in the preloader section i need to type in all the images... http://davidwalsh.name/mootools-image-preloading-progress-bar
is there a php code that will not have me do this?