4

I have a script that goes through a directory that has 3 images

$imglist='';
$img_folder = "path to my image";

//use the directory class
$imgs = dir($img_folder);

//read all files from the  directory, checks if are images and ads them to a list 
while ($file = $imgs->read()) {
  if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
    $imglist .= "$file ";
} 
closedir($imgs->handle);

//put all images into an array
$imglist = explode(" ", $imglist);

//display image
foreach($imglist as $image) {
  echo '<img src="'.$img_folder.$image.'">';
}

but the problem that I am having is it display a 4th img with no image.. yet I only have 3 image in that folder.

1
  • Test your code with a file named "this_is_text_not_png.txt" Commented Jul 8, 2011 at 18:23

4 Answers 4

6

There is no need to build a string of images and then explode that string into an array of images instead just add the images directly to an array as Radu mentioned. Here is the corrected code:

$imglist = array();
$img_folder = "path to my image";

//use the directory class
$imgs = dir($img_folder);

//read all files from the  directory, checks if are images and adds them to a list 
while ($file = $imgs->read()) {
   if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)){
      $imglist[] = $file;
   } 
}
closedir($imgs->handle);

//display image
foreach($imglist as $image) {
    echo '<img src="'.$img_folder.$image.'">';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I am try to make it load the list randomly by putting shuffle() but I have no luck I am trying to learn PHP as I go along so I do appreciate it.
If you have an error Directory::read() method not found - try pth_read() instead of read() - undocumented "feature"
2

You'll have a space at the end of the $imglist string, which explode() will turn into an empty element. Trim the string:

$imglist = explode(" ", trim($imglist));

Or better yet, just add them to the $imglist array in the first place, instead of making a string and exploding it:

$imglist = array();
/* ... */
$imglist[] = $file;

1 Comment

You can use shuffle() to randomize the contents of an array.
2

ereg() is deprecated. You'd probably be better off with:

chdir($img_folder);
$imgs = glob('*.jpg *.gif *.png');
foreach ($imgs as $img) {
    echo "<img src=\"{$img_folder}/{$img}\">";
}

glob() does wildcard matching pretty much the same way as most Unix shells do.

1 Comment

Furthermore, eregi was being used to match substrings anywhere on the file name.
0
Use [glob()][1] function
<?php
    define('IMAGEPATH', 'path to my image/'.$imglist.'/');
    foreach(glob(IMAGEPATH.'*.jpg') as $filename){
        echo '<img src="'.$filename.'" alt="'.$album.'" />';

    ?>

  [1]: http://www.php.net/manual/en/function.glob.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.