ProdigYu's Blog

PHP随机显示图片

by ProdigYu on Aug.26, 2011, under PHP

pic.php list.txt放在网站根目录

pic.php内容如下:
<?php
$arr=file('list.txt');
$n=count($arr)-1;
header('Location: http://prodigyu.com/pic/'.$arr[rand(0,$n)]);
?>

list.txt就是所有图片的列表
用命令

ls /图片所在文件夹 > list.txt

网上有代码是历编图片文件夹,然后随机选一个图片,这个方法比较耗费cpu
所以我先生成一个list,然后只需要读一个txt就可以了
速度比较快,也不占资源
如果想要方便查看,可以建一个index.html

<script language=javascript>
document.onkeydown=nextpage
function nextpage(event)
{
event = event ? event : (window.event ? window.event : null);
if (event.keyCode==39) window.location.reload();//右方向键
}
< /script>
< img src="http://prodigyu.com/pic.php" alt="" />

:, ,

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...