php随机图片源码

hirokoyo / 2023-07-31 / 原文

<?php
// 设置图片文件夹路径
$dir = 'images/';

// 获取文件夹中所有图片文件名
$files = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

// 随机选择一张图片
$random_file = $files[array_rand($files)];

// 设置响应头为图片类型
header('Content-Type: image/jpeg');

// 输出图片
readfile($random_file);
?>