Jump to content

[SOLVED] image page


Sir Softsand

Recommended Posts

Hi, I got a folder on my FTP called "images"

I have a page. I want to show all of the images in the directory image on that page without keep updating that page with HTML <img src="...

My friend said you could do this with PHP, is that true?

 

Thanks in advance, Sir Softsand

Link to comment
https://forums.phpfreaks.com/topic/54784-solved-image-page/
Share on other sites

Yes that is true. You can use pho's readdir function to read all the files/folders in the images folder. Then using a while loop you can echo out the HTML to display the images on your webpage.

 

Before you take on this project of yours make sure you know the basics of PHP. It will be better for you to learn the basics of PHP, such as variables, functions, loops, arrays, data types, superglobals ($_POST, $_GET etc) before you get started. Also make sure your hosting account has PHP enabled -check with your host.

<?php

$handle = opendir('/images');

    while ($file = readdir($handle)) {
            echo "<img src=\"$file\" alt=\"\" /><br />\n";
    }

closedir($handle);

?>

 

Untested, I suggest you read up on the functions used though :)

Sorry, I made a mistake, the dir's name is: avatars

This code doesn't work:

<?php

$handle = opendir('/avatars');

    while ($file = readdir($handle)) {
            echo "<img src=\"$file\" alt=\"\" /><br />\n";
    }

closedir($handle);

?>

Use the following path:

./avatars/

and not

/avatars

 

If you are running it on a UNIX based platform, PHP will treate / as the root (of the hard drive) and so it will try to access a folder called avatars in the root of the hard drive. The root will be most probably out of bounds to you. What you want to do is place a period before the slash (./) this will tell PHP to look in the current directory and not the root.

$handle = opendir('./images');

    while ($file = readdir($handle)) {
            echo "<img src=\"$file\" alt=\"\" /><br />\n";
    }

closedir($handle);

 

this works for me, but the images are not comming out.  When I click properties on the image, it shows www.mysite.com (IE the site I have them on) and nothing else.

Ok, I know i clicked solved, but now I got a problem. The avatars showed up every time. But I changed host, and just transferred all data from the other host I ad before. The avatars still showed up, but when I refreshed the page, 2 avatars dissapeared, when I refreshed again more avatars dissapeared, then I refreshed again and they were all gone!

Either give a link to the page where we can view the generated code, or post the generated code here.  We really can't tell what might be happening looking at php fragments, the source of your problem is within the generated html page.

 

Appearing and disappearing sounds like you're seeing cached versions.

I see a whole series of images.  The first two lines below obviously have a problem, the rest are fine.  Check your php code to see where the first two lines are coming from to fix them.

 

<br /><img src="avatars/." alt="" / > 

<img src="avatars/.." alt="" / > 
<img src="avatars/cirkels1.jpg" alt="" / > 
... more images follow

Here's my php code:

<?php

include('overall_header.html');

$handle = opendir('./avatars');

    while ($file = readdir($handle)) {
            echo "<img src=\"avatars/$file\" alt=\"\" / > \n";
    }

closedir($handle);

include('overall_footer.html');

?>

No. If they appear once and you don't change the code then they'll always appear (unless it's hosted on a dreadfully bad server and you have a really bad internet connection, in which case you might have problems seeing them).

 

The problem with the code you posted is that it doesn't ignore . and .., i.e. folder structure in the folder you're reading.  That's where the incorrect html code originates.  Take a closer look at user notes on the manual page on readdir - or use glob() instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.