php中随机显示图片的函数代码
程序员文章站
2022-11-27 17:19:03
例如博客的展示窗 复制代码 代码如下:
例如博客的展示窗
<?php
/**********************************************
* filename : img.php
* author : freemouse
* web : www.cnphp.info
* email :freemouse1981@gmail.com
* date : 2010/12/27
* usage:
* <img src=img.php>
* <img src=img.php?folder=images2/>
***********************************************/
if($_get['folder']){
$folder=$_get['folder'];
}else{
$folder='/images/';
}
//存放图片文件的位置
$path = $_server['document_root']."/".$folder;
$files=array();
if ($handle=opendir("$path")) {
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file;
}
}
}
closedir($handle);
$random=rand(0,count($files)-1);
if(substr($files[$random],-3)=='gif') header("content-type: image/gif");
elseif(substr($files[$random],-3)=='jpg') header("content-type: image/jpeg");
readfile("$path/$files[$random]");
?>
复制代码 代码如下:
<?php
/**********************************************
* filename : img.php
* author : freemouse
* web : www.cnphp.info
* email :freemouse1981@gmail.com
* date : 2010/12/27
* usage:
* <img src=img.php>
* <img src=img.php?folder=images2/>
***********************************************/
if($_get['folder']){
$folder=$_get['folder'];
}else{
$folder='/images/';
}
//存放图片文件的位置
$path = $_server['document_root']."/".$folder;
$files=array();
if ($handle=opendir("$path")) {
while(false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(substr($file,-3)=='gif' || substr($file,-3)=='jpg') $files[count($files)] = $file;
}
}
}
closedir($handle);
$random=rand(0,count($files)-1);
if(substr($files[$random],-3)=='gif') header("content-type: image/gif");
elseif(substr($files[$random],-3)=='jpg') header("content-type: image/jpeg");
readfile("$path/$files[$random]");
?>