列出目录(和子目录)中的所有图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列出目录(和子目录)中的所有图像相关的知识,希望对你有一定的参考价值。

This snippet will create an array of all images in a directory and sub-directories and display them randomly.
  1. <?php
  2. //path to directory to scan. i have included a wildcard for a subdirectory
  3. $directory = "images/*/";
  4.  
  5. //get all image files with a .jpg extension.
  6. $images = glob("" . $directory . "*.jpg");
  7.  
  8. $imgs = '';
  9. // create array
  10. foreach($images as $image){ $imgs[] = "$image"; }
  11.  
  12. //shuffle array
  13. shuffle($imgs);
  14.  
  15. //select first 20 images in randomized array
  16. $imgs = array_slice($imgs, 0, 20);
  17.  
  18. //display images
  19. foreach ($imgs as $img) {
  20. echo "<img src='$img' /> ";
  21. }
  22. ?>

以上是关于列出目录(和子目录)中的所有图像的主要内容,如果未能解决你的问题,请参考以下文章