dpfqy5976
dpfqy5976
2013-09-03 04:35
采纳率: 100%
浏览 51
已采纳

使用php循环html div来显示文件夹中的图像

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • dsoxcj7276
    dsoxcj7276 2013-09-03 04:39
    已采纳

    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' />
    
    点赞 评论
  • dounai6626
    dounai6626 2013-09-03 04:43

    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.

    http://us2.php.net/glob

    点赞 评论
  • douhoushou8385
    douhoushou8385 2013-09-03 04:45

    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>";
    }
    ?>
    
    点赞 评论
  • dpleylxzx47207117
    dpleylxzx47207117 2013-09-03 04:46

    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()

    点赞 评论
  • douliang2935
    douliang2935 2013-09-03 04:50

    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>";  
    }
    
    ?>
    
    点赞 评论
  • douba6361
    douba6361 2013-09-03 04:51

    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
    });
    
    点赞 评论
  • dongshao9106
    dongshao9106 2016-03-02 21:12

    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>";
    }
    ?>
    
    点赞 评论

相关推荐