Select random file from directory

后端 未结 4 1279
深忆病人
深忆病人 2020-11-30 04:43

I\'m trying to make a site where users can submit photos, and then randomly view others photos one by one on another page. I have a directory called \"uploads\" where the pi

4条回答
  •  既然无缘
    2020-11-30 05:22

    This single line of code displays one random image from the target directory.

    
    

    Target directory: /images/

    Image prefix: image_

    Number of images in directory: 127

    https://perishablepress.com/drop-dead-easy-random-images-via-php/


    Drawbacks

    • images must be named sequentially (eg image_1.png, image_2.png, image_3.png, etc).

    • you need to know how many images are in the directory in advance.


    Alternatives

    Perhaps there's a simple way to make this work with arbitrary image-names and file-count, so you don't have to rename or count your files.

    Untested ideas:

    • />

    • shuffle()

    • scanDir() with rand(1,scanDir.length)

提交回复
热议问题