How to get random image from directory using PHP

前端 未结 10 1723
我寻月下人不归
我寻月下人不归 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:34
    <?php
       foreach (glob("gallery/*") as $filename) {
             echo '<li><a href="'.$filename.'" title=""><img src="'.$filename.'" alt="" /></a> </li>';      
           }
    ?>
    

    Look at this code, use it definitely if useful for you. It loads all files from folder and prints them in above format. I made this code to use with lightbox.

    0 讨论(0)
  • 2020-12-02 19:39
    $imagesDir = 'images/tips/';
    
    $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $randomImage = $images[array_rand($images)]; // See comments
    

    You can send a 2nd argument to array_rand() to get more than 1.

    0 讨论(0)
  • 2020-12-02 19:41

    Agreed with alexa. Use simple function.

    function RandImg($dir)
    {
    $images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $randomImage = $images[array_rand($images)];
    return $randomImage;
    }
    
    $the_image = RandImg('images/tips/');
    echo $the_image;
    
    0 讨论(0)
  • 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...

    <?php
      ##########################################################
      # Simple Script Random Images Rotator • 1.4 • 04.01.2020 #
      # Alessandro Marinuzzi [alecos] • https://www.alecos.it/ #
      ##########################################################
      function rotate($folder) {
        if ((file_exists($_SERVER['DOCUMENT_ROOT'] . "/$folder")) && (is_dir($_SERVER['DOCUMENT_ROOT'] . "/$folder"))) {
          $list = scandir($_SERVER['DOCUMENT_ROOT'] . "/$folder");
          $fileList = array();
          $img = '';
          foreach ($list as $file) {
            if ((file_exists($_SERVER['DOCUMENT_ROOT']  . "/$folder/$file")) && (is_file($_SERVER['DOCUMENT_ROOT']  . "/$folder/$file"))) {
              $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
              if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png') {
                $fileList[] = $file;
              }
            }
          }
          if (count($fileList) > 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):

    <a href="/<?php include("php/rotate.php"); echo $my_gallery; ?>"><img src="/<?php echo $my_gallery; ?>" alt="Random Gallery" width="90" height="67"></a>
    

    Other usage using FancyBox library:

    <a href="/<?php include("php/rotate.php"); echo $my_gallery; ?>" data-fancybox><img src="/<?php echo $my_gallery; ?>" alt="Random Gallery" width="90" height="67"></a>
    

    Hope this Helps.

    0 讨论(0)
  • 2020-12-02 19:49

    Simpler:

    $directory = "medias/photos/";
    $img = glob($directory . "*.jpg");
    shuffle($img);
    
    0 讨论(0)
  • 2020-12-02 19:51

    You can use opendir() to read in the filenames from that directory, storing each filename in an array. Then use rand() with a min and max corresponding to your array keys to select an item from the array.

    0 讨论(0)
提交回复
热议问题