Fixing this one

Discussion in 'PHP' started by jpinheiro, Aug 9, 2008.

  1. #1
    Ok i cant get $image to be defined

    It is supposed to read the directory its in for the specified file type :

     foreach (glob("*.jpg") as $image); 
    PHP:

    there are multiple image of the same filetype in the directory

    what is wrong with the code?

    
    
    <?php
    foreach(glob("*.gif") as $image); 
      echo '<img src="./$image" /></td>';
    
    ?>
    <br>
    
    <?php
    foreach(glob("*.jpg") as $image); 
      echo '<img src="./$image" /></td>';
    
    ?>
    <br>
    
    <?php
    foreach(glob("*.jpeg") as $image); 
      echo '<img src="./$image" /></td>';
    
    ?>
    <br>
    
    <?php
    foreach(glob("*.png") as $image); 
      echo '<img src="./$image" /></td>';
    
    ?>
    <br>
    
    <?php
    foreach(glob("*.PNG") as $image); 
      echo '<img src="./$image" /></td>';
    
    ?>
    
    
    PHP:
    ;
     
    jpinheiro, Aug 9, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    lol :) nice
     
    php-lover, Aug 10, 2008 IP
  4. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, I'm pretty sure you're wanting to display the filename of several files, so you want to use this, otherwise you'll only echo the info for one image - the last image found:
    <?php
    
    $images = glob("/path/to/images/{*.gif,*.jpg,*.jpeg,*.png,*.PNG}", GLOB_BRACE);
    
    foreach($images as $image) {
      echo '<img src="./$image" /></td>';
    }
    
    ?>
    PHP:
     
    Pos1tron, Aug 10, 2008 IP