php中随机显示图片的函数代码

yipeiwu_com6年前PHP代码库
例如博客的展示窗
复制代码 代码如下:

<?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通过sort()函数给数组排序的方法

本文实例讲述了php通过sort()函数给数组排序的方法。分享给大家供大家参考。具体分析如下: sort()函数用于给数组排序,本函数为数组中的单元赋予新的键名。原有的键名将被删除。...

php实现xml与json之间的相互转换功能实例

本文实例讲述了php实现xml与json之间的相互转换功能。分享给大家供大家参考,具体如下: 用php实现xml与json之间的相互转换: 相关函数请查看php手册。 一、参考xml如下...

PHP使用strtotime计算两个给定日期之间天数的方法

本文实例讲述了PHP使用strtotime计算两个给定日期之间天数的方法。分享给大家供大家参考。具体分析如下: PHP的strtotime函数用于将任何英文文本的日期时间描述解析为Uni...

PHP最常用的2种设计模式工厂模式和单例模式介绍

1.工厂模式 主要作用是降低耦合度。 复制代码 代码如下: abstract class Operation{ abstract public function getValue($nu...

深入apache配置文件httpd.conf的部分参数说明

<Directory>...</Directory> -- 设定指定目录的访问权限<Files>...</Files> -- 设置应用于指...