◇家电维修技术论坛◇'s Archiver

qijisky 发表于 2007-4-22 17:47

随机取图片的PHP代码

将以下代码存为PHP文件,然后在同级目录下建立图片目录放入图片


[code]

<?
$path='image'; //图片目录
$photo=array();//创建一个数组$photo,大小待定
$i=0; //用于给$photo数组附值,初始化0
if(!is_dir($path))
{
mkdir($path);
}
else
{
if($handle=opendir($path))
{
while(($file=readdir($handle))!==false)
{
if($file!=='.'&&$file!=='..')
{
$photo[$i]=$file;//将读取的文件名存入数组中.
$i++;
}
}
closedir($handle); //关闭目录
}
$random=rand(0,count($photo)-1); //产生数组photo下标的随机数
$ext=substr($photo[$random],-3); //得到随即从$photo数组中提取的图片的扩展名
if($ext=='gif') header('content-type: image/gif');
elseif($ext=='jpg')header('content-type: image/jpeg');
elseif($ext='png')header('content-type: image/png');
readfile("$path/$photo[$random]");
}
?>[/code]

页: [1]


Google


Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.