首页 > 解决方案 > 随机 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。

标签: phpfile-get-contents

解决方案


好吧,如果您只是显示来自同一文件夹的图像,就像您的 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


推荐阅读