need help creating a very basic php script to display images from a directory

Discussion in 'PHP' started by js09, Jul 20, 2009.

  1. #1
    simply put i have a directory of images on my ftp. all images are already formatted. i just need a simple script to display them on a webpage.

    the ONLY constraint i need is that there is a few img breaks (<br><br><br>) after each image.

    that's it, just a simple list of images.

    thanks for any help.
     
    js09, Jul 20, 2009 IP
  2. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    are you pulling them from a database?

    If you do you will need a while loop for that and a <table>
     
    co.ador, Jul 20, 2009 IP
  3. js09

    js09 Peon

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nope, just images within a folder. the php code will pull them by their i suppose
     
    js09, Jul 20, 2009 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    try the following code.

    $dir = "/images";
    
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
    
               echo  '<img src ="'.$dir.$file.'"> <br><br><br>';  
    
          }
            closedir($dh);
        }
    }
    
    PHP:
     
    php-lover, Jul 21, 2009 IP
  5. js09

    js09 Peon

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5

    hmm not working. i just throw this code in a php file and upload it into the same folder as my images?

    little confused.. thanks.
     
    js09, Jul 23, 2009 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    If you put this file in the same directory as your images, then change this line: $dir = "/images";

    to this:

    $dir = "./";

    haven't test it but give a try.
     
    php-lover, Jul 24, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    replace $dir = "/images"; with

    
    $dir = realpath("./")."/images";
    
    PHP:
    Assuming that there's a folder called images in the same folder as the php script.

    However stick with relative path when writing it to the html.
     
    kblessinggr, Jul 24, 2009 IP