0

I have the following code that reads the content of a folder

PHP Code:

<?
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');

//This function gets the file names of all images in the current directory
//and ouputs them as a JavaScript array
function returnimages($dirname="./images") {
     $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
     $files = array();
     $curimage=0;
           if($handle = opendir($dirname)) {
                while(false !== ($file = readdir($handle))){
                     if(eregi($pattern, $file)){ //if this file is a valid image 
//Output it as a JavaScript array element
                   echo 'galleryarray['.$curimage.']="'.$file .'";' . "\n";
             $curimage++;
                    } 
            }

            closedir($handle);
}
        sort($files);
        return($files);
}

echo 'var galleryarray=new Array();' . "\n"; //Define array in JavaScript
returnimages() //Output the array elements containing the image file names

?>

which spits out this the following code:

var galleryarray = new Array();
galleryarray[0] = "image1.jpg";
galleryarray[1] = "image5.jpg";
galleryarray[2] = "image2.jpg";
galleryarray[3] = "image4.jpg";
galleryarray[4] = "image8.jpg";
galleryarray[5] = "image7.jpg";
galleryarray[6] = "image0.jpg";
galleryarray[7] = "image3.jpg";
galleryarray[8] = "image6.jpg";​

Now in my html file I want to echo something like

<img src="images/image0.jpg" />
<img src="images/image1.jpg"/>
...
... 

How can I do that?

4 Answers 4

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

Comments

0

maybe this help u

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image extensions
           if($handle = opendir("./images")) {
                while(false !== ($file = readdir($handle))){
                     if(eregi($pattern, $file)){ ?>
                   <img src="/images/<?php echo $file;  ?>">
            <?php 
                    } 
            }
           }

Comments

0

assuming that you want to sort image name by value and then show the images. you first need to sort values by using function "asort"

    asort($galleryarray);
    foreach ($galleryarray as $key => $val) {
        echo "<img src='images/{$val}'/>"
    }

if you want to print and array

  1. print_r($array); or if you want nicely formatted array then

    echo ''; print_r($array); echo '';

  2. use var_dump($array) to get more information of the content in the array like datatype and length.

  3. you can loop the array using php's foreach(); and get the desired output. more info on foreach in php's documentation website https://www.php.net/manual/en/control-structures.foreach.php

Comments

0

Try changing the echo line in the function to this:

echo '<img src="images/' . $file . '" />' ;

Hope this will help :)

OR

Outside the function, use it like this:

$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
  echo '<img src="images/' . $img . '" />';
}

Also, you have to include this line inside the if condition in the while loop:

$files[] = $file; //insert the file into the array. This array is what we are going to return from the function

Update

I have tested this code and it's working:

    //PHP SCRIPT: getimages.php
    header('content-type: application/x-javascript');

    function returnimages($dirname="./images") {
         $pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)";     //  http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/
         $files = array();
         if($handle = opendir($dirname)) {
            while(false !== ($file = readdir($handle))){
                if(preg_match($pattern, $file)){ //if this file is a valid image 
                    $files[] = $file;
                } 
            }

            closedir($handle);
        }
        //sort($files);         // http://php.net/manual/en/function.sort.php
        natcasesort($files);    // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php

        return($files);
    }

    $images = returnimages(); //will get the array containing the images
    foreach($images as $img)
    {
      echo '<img src="images/' . $img . '" />';
    }

In my images folder, I have put 5 files for testing. And upon running it, it would give the following:

<img src="images/a.jpg" />
<img src="images/ba.jpg" />
<img src="images/ca.jpg" />
<img src="images/Copy (3) of a.jpg" />
<img src="images/Copy (4) of a.jpg" />

4 Comments

is there a way to sort the file name alphabetically?
no i tried sort($file) and sort($files) it didnt work. where should i put them?
the code works great. how can i print or echo the output in my html file?
The last 5 lines of code will do the trick. First we are calling the function returnimages() and then assigning the array returned by the function into a variable $images. This would be an array. The next line, that is the foreach loop will iterate through each element in the array. Inside the loop, I'm echoing the images wrapping HTML img tags.

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.