php 从目录中选择随机文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4478783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:08:53  来源:igfitidea点击:

Select random file from directory

php

提问by alexn

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 pictures are submitted. I'm having trouble reading the pictures from the file. I just want to randomly select a picture from the directory uploads and have it displayed on the page. Any suggestions appreciated.

我正在尝试制作一个网站,用户可以在其中提交照片,然后在另一个页面上一张一张地随机查看其他人的照片。我有一个名为“上传”的目录,用于提交图片。我在读取文件中的图片时遇到问题。我只想从上传的目录中随机选择一张图片并将其显示在页面上。任何建议表示赞赏。

回答by alexn

You can use globto get all files in a directory, and then take a random element from that array. A function like this would do it for you:

您可以使用glob获取目录中的所有文件,然后从该数组中获取随机元素。像这样的函数可以为您完成:

function random_pic($dir = 'uploads')
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}

回答by Naveen Web Solutions

I've turned it a little to get more than one random file from a directory using array.

我已经稍微改变了一点,以使用数组从目录中获取多个随机文件。

<?php

function random_pic($dir)
{
 $files = glob($dir . '/*.jpg');
 $rand_keys = array_rand($files, 3);
 return array($files[$rand_keys[0]], $files[$rand_keys[1]], $files[$rand_keys[2]]);
}

// Calling function

list($file_1,$file_2,$file_3)= random_pic("images"); 

?>

You can also use loop to get values.

您还可以使用循环来获取值。

回答by Naveen Web Solutions

Or you can use opendir()instead of glob() because it's faster

或者你可以使用opendir()而不是 glob() 因为它更快

回答by johny why

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

这一行代码显示了目标目录中的一个随机图像。

<img src="/images/image_<?php $random = rand(1,127); echo $random; ?>.png" />

Target directory:/images/

目标目录:/images/

Image prefix:image_

图片前缀:image_

Number of images in directory:127

目录中的图像数:127

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

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.

  • 图像必须顺序命名(例如image_1.pngimage_2.pngimage_3.png等等)。

  • 您需要提前知道目录中有多少图像。



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:

未经测试的想法:

  • <img src=<?php $dir='/images/'; echo $dir . array_rand(glob($dir . '*.jpg')); ?> />

  • shuffle()

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

  • <img src=<?php $dir='/images/'; echo $dir . array_rand(glob($dir . '*.jpg')); ?> />

  • shuffle()

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