源来页 公告 登陆

PHP实现指定文件夹路径随机图片功能

南墙 @Ta
2020-05-02 22:23

可根据实际情况修改images图片路径,文件名通配的,因此不必担心要改什么特定格式文件名,后缀符合下述即可
<?php
$img_array = glob('images/*.{gif,jpg,png,jpeg,webp,bmp}', GLOB_BRACE);
if(count($img_array) == 0) die('没找到图片文件。请先上传一些图片到 '.dirname(__FILE__).'/images/ 文件夹');
header('Content-Type: image/png');
echo(file_get_contents($img_array[array_rand($img_array)]));
?>

当然,有人可能觉得放本地服务器太占内存了,可以这样,其中img.txt为图片外链,以换行分隔

<?php
$arr=file('img.txt');
$n=count($arr)-1;
for ($i=1;$i<=1;$i++){
  $x=rand(0,$n);
  header("Location:".$arr[$x],"\n");
}
?>

来源:https://cway.top/post/698.html
阅:1586 发表于2020-05-02 22:23
#php源码
评论:

返回源来页/返回首页


天合之作 曲在人意

心灵鸡汤:
      

®2019-2024 all power by Zlproject3.7
川公网安备 51192202000151号
蜀ICP备2021014921号-2 首页 #top

本网站由提供CDN加速/云储存服务

天合之作 曲在人意