【发布时间】:2018-01-02 19:36:14
【问题描述】:
我在一个文件(称为 random.php)中有这个脚本,它显示了它所在文件夹中的随机图像:
<?php
$pics = glob('*.jpg', GLOB_NOSORT);
$pic = $pics[array_rand($pics)];
header("Content-type: image/jpeg");
header("Content-Disposition: filename=\"" . basename($pic) . "\"");
readfile($pic);
?>
我这样称呼它:
<img class="random" src="http://www.example.com/random.php" />
它工作正常。
我想通过从一个充满行的文本文件中提取它们的 URL 来显示随机图片,每一行都是一个图像 URL。 如何做到这一点?
最终更新:这对我有用。
<?php
$file = 'random.txt';
if (!is_readable($file)) {
exit('File list does not exist, or is not readable by the webserver.');
}
$pics = file('random.txt', FILE_SKIP_EMPTY_LINES);
$pic = $pics[array_rand($pics)];
if (!getimagesize($pic)) {
exit('Image does not exist, or is not readable by the webserver.');
}
/// content type
header('Content-Type: image/jpeg');
// prevent caching (so its random)
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
//
readfile($pic);
?>
Lawrence Cherone 在下面精心编译的脚本,但注释掉了最后一个标题标签。
【问题讨论】: