Page 1 of 1

read a folder anf display images and diplay

Posted: Tue Apr 28, 2009 8:08 am
by nicademus
Hello All!
I am close to getting this script to work but have hit a roadblock. I am trying to get the images from a folder to be stored in separate variables - something like a mysql_fetch_array for images in a folder.

This is what I have now:

Code: Select all

 
 
<?php
$path = "img/images/co_images/playfulpromises/small/";
$dirname = "img/images/co_images/playfulpromises/small/";
$images = scandir($dirname);
 
       
foreach($images as $file)
  {
  if ($file != "." && $file != "..") 
    {
     $files[] = $file;
    }
  $size_array = count($file);
  $i=0;
  while($i<$size_array)
   {
     
   ?>
 
    <img src="<?php echo $path ?><?php echo $file ?>" /><br /><br />
 
    <?php
   $i++;
   }
}  
?>
 
 
It displays all of the images but I cannot figure out how to get each image into a separate variable to be called into an <li> as an img src.


Thanx a million!

Re: read a folder anf display images and diplay

Posted: Tue Apr 28, 2009 9:52 am
by ben.artiss
Is this the kind of thing you had in mind?

Code: Select all

<?php
$path = "img/images/co_images/playfulpromises/small/";
$images = scandir($path);
$files = array();
 
foreach ($images as $file) {
    if ($file != "." && $file != "..") {
        $files[] = $file;
    }
}
 
if (count($files)>0) {
    echo '<ul>';
    foreach ($files as $file) {
        echo '<li><img src="'.$path.$file.'" alt="" /></li>'."\n";
    }
    echo '</ul>';
}
?>
Hope that points you in the right direction if you haven't fixed it already :)