php获取随机图片,PHP 随机显示某张图片

随机显示指定图像文件夹下的所有图片

# Init Array

$files = array();

# Get Folder

if($_GET['folder']) {

$folder = $_GET['folder'];

} else {

# Set Default Folder

$folder = '/img/';

}

# Set Full Path

$path = $_SERVER['DOCUMENT_ROOT'] . '/' . $folder;

# Open Directory

if($handle = opendir($path)) {

# Loop Through Directory

while(false !== ($file = readdir($handle))) {

if ($file != "." && $file != "..") {

if(substr($file, -3) == 'gif' || substr($file, -3) == 'png' || substr($file, -3) == 'jpg' || substr($file, -4) == 'jpeg') $files[count($files)] = $file;

}

}

}

# Close Handle

closedir($handle);

# Init Random

$rand = rand(0, count($files)-1);

# Check Header Type

# GIF

if(substr($files[$random], -3) == 'gif') header("Content-type: image/gif");

# JPEG

elseif(substr($files[$random], -3) == 'jpg') header("Content-type: image/jpeg");

elseif(substr($files[$random], -4) == 'jpeg') header("Content-type: image/jpeg");

# PNG

elseif(substr($files[$random],-3) == 'png') header("Content-type: image/png");

# Read File

readfile($path . '/' . $files[$random]);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值