Results 1 to 4 of 4

Thread: write images from dir

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default write images from dir

    I have the code below which i got from the looking for a code or script board but no one there is answering now. The code is suppose to read my directory called images and write <a href="image# in directory"><img scr="image# in directory"></a>
    Then keep writing till it gets to the last image. This is the code I got but it is only writing 1 comment tag(<!-- ... -->) . i have about 50 images in the images directory and 3 other directories with in that directory would that make an error?
    http://www.bluewalrus.net/testing.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
      define('IMG_DIR', 'images/');
    
      function is_image($fn) {
        return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
      }
      $imgs = array();
      $dir = opendir(IMG_DIR);
      while ($ff = readdir($dir))
        if (is_image($f))
          $imgs[] = IMG_DIR . '/' . $f;
      closedir($dir);
    ?>
    <!-- ... -->
    <?php foreach ($imgs as $img) { ?>
      <a href="$img">
        <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
      </a>
    <?php } ?>
    </body>
    </html>
    Any ideas or suggestions i appreciate thanks.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You had typos and errors:
    • Code:
      while ($ff = readdir($dir))
          if (is_image($f))
            $imgs[] = IMG_DIR . '/' . $f;
      $f does'nt exist, you mean $ff instead.

    • You define IMG_DIR with 'images/' string, so this part makes it erroneous:
      Code:
      $imgs[] = IMG_DIR . '/' . $f;
      ...it will become images//

    • You need to place the variables inside the PHP code:
      Code:
      <a href="$img">
      <img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">


    The rectified version is:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
      define
    ('IMG_DIR''images');

      function 
    is_image($fn) {
        return 
    in_array(pathinfo($fnPATHINFO_EXTENSION), array('jpg''jpeg''png''gif'));
      }
      
    $imgs = array();
      
    $dir opendir(IMG_DIR);
      while (
    $ff readdir($dir))
        if (
    is_image($ff))
          
    $imgs[] = IMG_DIR '/' $ff;
      
    closedir($dir);
    ?>
    <?php 
    foreach ($imgs as $img
        {
        echo 
    '<a href="'.$img.'">
            <img src="'
    .$img.'" alt="'.pathinfo($imgPATHINFO_FILENAME).'">
            </a>'
    ;
        }
    ?>
    </body>
    </html>
    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Oh oh. Thanks that's the closest i've been thus far but thats only bringing up one image from the directory and this error message
    Warning: pathinfo() expects parameter 2 to be long, string given in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 25.
    Any Ideas?

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    For anyone looking to write all the images in a directory with a link to the file...

    The script for reading images directory and writing the image with a link to the actual file...

    PHP Code:
    <?php
      
    //getting all files of desired extension from the dir using explode
      
    $dirname "images/";
      
    $dir opendir($dirname);
      while(
    false != ($file readdir($dir)))
      {
        if((
    $file != ".") and ($file != ".."))
        {
          
    $fileChunks explode("."$file);
          if(
    $fileChunks[1] == 'jpg' || 'jpeg' || 'gif' || 'png'//File types to be read
          
    {      
            echo 
    '<a href="images/'.$file.'" ><img src="images/' $file '" border="0" /></a></br>';
          }
        }
      }
      
    closedir($dir);
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •