php脚本,用于显示从文本文件中提取URL的随机图像
我在一个名为random.php的文件中有这个脚本,该文件显示了它所在文件夹中的随机图像:php脚本,用于显示从文本文件中提取URL的随机图像,php,image,random,Php,Image,Random,我在一个名为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);
<?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);
?>
下面的脚本是由劳伦斯·切龙编辑的,但注释掉了最后一个标题标签。切换出glob'*.jpg',glob_NOSORT;因为
$pics=file'/path/to/file.txt',file\u跳过\u空行
尝试此操作,并添加错误检查:
<?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");
//
header('Content-Disposition: filename="'.basename($pic).'"');
readfile($pic);
?>
不要在注释中添加代码,而是用您的确切代码更新您的问题。你需要调试它,因为发生的是你的输出空白或文件抛出了一个错误,可能是权限或找不到,当设置头时抛出一个错误。正如你所建议的,我用需要调试的代码更新了我的原始帖子。你能按原样写出完整的剧本吗?我只知道复制和粘贴脚本,谢谢您的理解。@Lawrence random.txt的内容只有两行,每行都是托管在google@Konstantinos请参阅更新的代码,我已经对其进行了测试,效果良好。@Konstantinos添加了一些额外的标题以停止浏览器缓存。