drrc61668568 2012-03-27 04:22
浏览 16
已采纳

显示文件夹中的所有图像,而不测试文件是否存在

Using the following code, I am able to display up to 10 images. What I want is to be able to display all images in the folder without going through an infinite loop and without testing if the image file exist at each loop.

for($i=1; $i<=10; $i++)
{
 $filename = "/folder/$i.jpg";
 echo "
  <img title='$i' src='$filename'>
 ";
}

Assuming that I have no idea how many images there are in the folder and that I do not want to store this information in a database, how would you display all the images with file names are that are integers ending with a .jpg extension?

  • 写回答

2条回答 默认 最新

  • donglu9896 2012-03-27 04:26
    关注
    //path to directory to scan
    
    $directory = "/folder/";
    
    //get all image files with a .jpg extension.
    
    $images = glob($directory . "*.jpg");
    

    UPDATE reference: http://php.net/manual/en/function.glob.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?