Question

I am trying to display the single image from the webroot folder say: banner. Putting it in an array. But what happens here is, it is displaying two images with same name.

My code is here:

<div id="banner-left-content">
<?php
    $ab[]= array();
    foreach(glob('./banner/thumbs/*.*') as $filename){
        $ab = array($filename);
        foreach($ab as $imagename) {
            $listImages[]=$imagename;
            echo "<img src='$listImages[0]' width='200' height='300'/>"; 
        }  
    }
?>
</div> 
Was it helpful?

Solution 3

Put your echo image outside of your foreach.

<div id="banner-left-content">

     <?php
        $ab[]= array();
        foreach(glob('./banner/thumbs/*.*') as $filename){
            $ab = array($filename);
                foreach($ab as $imagename) {
                    $listImages[]=$imagename;
                }  
        }

        echo "<img src='$listImages[0]' width='200' height='300'/>"; 
     ?>

</div> 

OTHER TIPS

<div id="banner-left-content">
    <img src="<?= glob('./banner/thumbs/*.*')[0] ?>" />
</div>

Try like

foreach(glob('./banner/thumbs/*.*') as $filename){
    echo "<img src='$filename' width='200' height='300'/>"; 
}

If you need only a single image from the array then you need to do like

foreach(glob('./banner/thumbs/*.*') as $filename){
    $ab = array($filename);
     foreach($ab as $imagename) {
         $imgArr[] = $imagename;
     } 
}
echo "<img src='$imgArr[0]' width='200' height='300'/>"; 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top