0

I have a folder "blah" that contains a lots of images.

I want to show all my images into my page.

Here my html code:

<div class="cp-thumb cp-masonry cp-thumbs616 masonry-brick">
   <img src="blah/225cd24c144611e3b69022000a1deb4b_7.jpg" class="img_235x235" />
</div>

When I use php for loop to show all the image, it does not work:

<?php
for($i=0; $i<=10; $i++)
{
   echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
       <img src='blah/225cd24c144611e3b69022000a1deb4b_7.jpg'class='img_235x235' />
     </div>";      
}     
?>

Anyone can help? Thanks

3
  • Are you sure that img src is right? Commented Sep 3, 2013 at 4:37
  • And why are you posting the same image 10 times? Commented Sep 3, 2013 at 4:37
  • I just try to loop the div but it does not loop at all. I dont know how to loop through the folder to show all image that is why i am asking here. Commented Sep 3, 2013 at 4:39

7 Answers 7

1

Try with GLOB like

foreach(glob('blah/'.'*') as $filename){
    echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
              <img src='".$filename."' class='img_235x235' />
          </div>"; 
}

COnsidered that all the image names are different and also the directory contains only images.

In my local I have images in my proj/blah/ So I have done like

<img src='proj/".$filename."' class='img_235x235' />
Sign up to request clarification or add additional context in comments.

2 Comments

I dont knw why my images didnt come out.. =(
You are getting image names..??May be you need to add your hosting and main folder names..I have checked in my local and it was working fine
1

It is simple. Just copy and paste

<?php

$dir = dir("blah");

while($filename=$dir->read()) {

    if($filename=="." || $filename=="..") continue;

    echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
       <img src='blah/".$filename."'class='img_235x235' />
     </div>";  
}

?>

Comments

0

use the glob function if you want to display all images on a folder.

foreach (glob("*") as $filename)
{
    echo '<div class="cp-thumb cp-masonry cp-thumbs616 masonry-brick">
              <img src="blah/'.$filename.'" class="img_235x235" />
          </div>';


}

keep in mind this function returns an array, so if you do not want to out put them all you have the ability to configure that, please read this.

https://www.php.net/glob

Comments

0

use glob to read images

<?php
foreach (glob("blah/*.jpg") as $filename) {
    echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'><img src='blah/". $filename . "'class='img_235x235' /></div>";
}
?>

Comments

0

I can propose a jQuery solution. Add an image slider that will pick up all the images directly from the folder.

Includes

<!-- jQuery library (served from Google) -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- directorySlider Javascript file -->
<script src="/js/directorySlider.js"></script>

HTML

<div id='slider-main' class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'></div>

JS

$('#slider-main').directorySlider({
    animation: 'fade',
    filebase: 'slide_',
    directory: 'blah/',
    extension: 'jpg',
    numslides: 10,
    height: 200
});

Comments

0

You need to check the directory path of blah folder

Try this,

foreach(glob('blah/{*.gif,*.jpg,*.png}',GLOB_BRACE) as $img){// you can add more extension here
    echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'>
       <img src='blah/".$img."'class='img_235x235' />
     </div>";
}

Read Glob()

Comments

0

Worked for me just fine tysm.

<?php
foreach (glob("blah/*.jpg") as $filename) {
    echo "<div class='cp-thumb cp-masonry cp-thumbs616 masonry-brick'><img src='blah/". $filename . "'class='img_235x235' /></div>";
}
?>

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.