php - 随机 URL 文件获取内容 PHP
问题描述
我试图使用文件获取内容来旋转 url,但几乎没有我无法弄清楚的错误。
<?php
//Rotate
$urls = array();
$divs[] = '/fun.jpg';
$divs[] = '/nuf.jpg';
echo file_get_contents(src="'. $divs[rand(0, count($divs)-1)] .'");
?>
但它没有返回任何随机 div。
解决方案
好吧,如果您只是显示来自同一文件夹的图像,就像您的 PHP 脚本所在的那样,您应该可以这样做。(没有测试它)
<?php
header("Content-Type: image/jpg");
$divs[] = 'fun.jpg';
$divs[] = 'nuf.jpg';
echo file_get_contents($divs[array_rand($divs)]);
?>
您需要包含标题:Show image using file_get_contents
对于从数组中选择随机键,您可以使用 array_rand() 函数:https ://www.php.net/manual/en/function.array-rand.php
推荐阅读
- javascript - 将具有级别信息的平面数组重新排列到具有子级的 n 叉树中
- django - 计算 Django ArrayField 中的不同元素
- java - 获取两个图像之间差异的图像(Java Graphics2D)
- 4d-database - ORDA:如何根据字段值查询字段值?
- android - android:创建一个完全透明的视图
- reactjs - 用跨度替换文本
- nestjs - Nestjs + fastify-http-proxy 的问题
- html - 如何知道有多少元素重叠并改变它们的宽度
- php - 将多张图片上传到 MySql 数据库
- c# - 相同的操作但不同的默认参数