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
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.

Link to comment
Share on other sites

<?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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.