简体   繁体   English

随机 URL 文件获取内容 PHP

[英]Random URL File Get Content PHP

i was trying to rotate url using file get content but there's little error which I am not able to figure out.我试图使用文件获取内容来旋转 url 但有一点我无法弄清楚的错误。

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

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

    ?>

But it's not returning any random div.但它没有返回任何随机 div。

Well, if you just show images from same folder, as your PHP script is in, you should be able to do it like this.好吧,如果你只是显示来自同一文件夹的图像,因为你的 PHP 脚本在,你应该可以这样做。 ( did not test it ) 没有测试它

<?php
header("Content-Type: image/jpg");

$divs[] = 'fun.jpg';
$divs[] = 'nuf.jpg';

echo file_get_contents($divs[array_rand($divs)]);
?>

You need to include header: Show image using file_get_contents您需要包含 header: 使用 file_get_contents 显示图像

And for picking random key from array you can use array_rand() function: https://www.php.net/manual/en/function.array-rand.php对于从数组中选择随机密钥,您可以使用 array_rand() function: https://www.php.net/manual/en/function.array-rand.ZE1BFD762321E409CEE4AC0B6E84196

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM