How to get random image from directory using PHP

前端 未结 10 1770
我寻月下人不归
我寻月下人不归 2020-12-02 19:16

I have one directory called images/tips.

Now in that directory I have many images which can change.

I want the PHP script to read the directory, to find the

10条回答
  •  星月不相逢
    2020-12-02 19:45

    I wrote a simple php script for my personal use. Now I want share it with stackoverflow's community. Usage is simple: create a folder "php" into root of your Web Server and put inside this file php rotate.php... now create two folders into your root called "pic" and "xmas"... you can adjust the folder names by editing the var $my_folder_holiday and $my_folder_default...

     0) {
            $imageNumber = time() % count($fileList);
            $img = $folder . '/' . $fileList[$imageNumber];
          }
          return $img;
        } else {
          mkdir($_SERVER['DOCUMENT_ROOT'] . "/$folder", 0755, true);
        }
      }
      $my_gallery_month = date('m');
      $my_folder_default = 'pic';
      $my_folder_holiday = 'xmas';
      if ($my_gallery_month == 12) {
        $my_gallery = rotate($my_folder_holiday);
      } else {
        $my_gallery = rotate($my_folder_default);
      }
    ?>
    

    This script was tested under PHP 7.0/7.1/7.2/7.3 and PHP 7.4 and works fine. Usage (for example in root you may have a folder "pic" and "xmas" containing your images):

    ">Random Gallery
    

    Other usage using FancyBox library:

    " data-fancybox>Random Gallery
    

    Hope this Helps.

提交回复
热议问题