Random URL File Get Content PHP

问题描述 投票:-1回答:1

我正在尝试使用文件获取内容来旋转url,但是几乎没有错误,我无法弄清楚。

    <?php
    //Rotate
$urls = array();
$divs[] = '/fun.jpg';
$divs[] = '/nuf.jpg';

echo file_get_contents(src="'. $divs[rand(0, count($divs)-1)] .'");

    ?>

但是它不返回任何随机的div。

php file-get-contents
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.