Click to See Complete Forum and Search --> : [RESOLVED] Displaying an Array of images


trevzilla
03-28-2008, 02:53 PM
I have a folder that people are going to upload images too. I would like to be able to display all the images in this folder. Problem is, the number of images is going to increase as time goes on, so I can't simply do a <img src="URL"> tag.

Here's what I have so far. Some php code that prints the names of the files in the directory.
<?php
$dir = "/home/content/t/r/e/trevzilla/html/uploads/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$images[] = $filename;
}
sort($images);
print_r($images);
?>

I'm just having a little difficulty making the leap from displaying the names of the array to displaying the pictures themselves. Anybody have any suggestions?

jasonahoule
03-28-2008, 02:57 PM
for($i=0; $i<count($images); $i++) {
echo "<img src=\"".$images[$i]."\">";
}

trevzilla
03-28-2008, 03:18 PM
Thanks for the fast reply!

That code makes total sense to me, however, it doesn't seem to be working. For some reason, the only picture that is being displayed is the smallest one. I did a few little modifications to your code, but it seemed that no matter what I did, I could only get one picture to show up. Here's my code now.

<?php
$dir = "/home/content/t/r/e/trevzilla/html/uploads/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$images[] = $filename;
}
sort($images);
print_r($images);
$count = count($images);
print_r($count);
for($i=2; $i<$count; $i++) {
echo "<img src=\"".$images[$i]."\"><br>";
}
?>

I put $i=2 in the for statement, because $images[0]=. and $images[1]=.. the real images begin on $images[2].

Any ideas why I'm only getting one picture to show up?

SyCo
03-28-2008, 03:21 PM
or using foreach


foreach($images as $v){
echo '<img src="'.$v.'"><br>';
}

Znupi
03-28-2008, 03:50 PM
Try:

for ($i=2; $i<count($images); $i++) {
echo '<img src="' . urlencode($images[$i]) . '">';
}

trevzilla
03-29-2008, 04:29 PM
Hmm, tried both of those to no avail. Same problem is happening. Just one picture is showing up. I don't even know why it deciedes to show the picture it does. . .

What may help is this. In firefox, only that one picture shows up. In Internet Explorer spaces for all the pictures show up, but they show up as the little broken link pictures. You know, with the little red x inside a picture frame looking thing. Any thoughts?

Znupi
03-30-2008, 08:16 AM
Look through the source code of the page (Ctrl+U in Firefox) and see if there's anything wrong with the images' urls.

trevzilla
03-30-2008, 12:12 PM
Ah, I see the problem now. Thanks for the insight. I just don't quite know how to fix it still. . .

The URL's are pointing to my home directory instead of the 'uploads' directory. (I have a copy of one picture in both home and upload directories, that is why that one picture is showing up. . .)

So, I guess does anyone know how to get the URL's to point to the Upload directory now.

Thanks!

trevzilla
03-30-2008, 01:24 PM
Got it! Thanks everyone! Here is the final code that I have used.

<?php
$dir = "/home/content/t/r/e/trevzilla/html/uploads/";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$images[] = $filename;
}
sort($images);
for($i=2; $i<count($images); $i++) {
echo '<img src="/uploads/'.$images[$i].'">';
}
?>

Just for anyone that may have the same problem.