php中随机显示图片的函数代码
作者:bea
例如博客的展示窗 代码如下: <?php /********************************************** * Filename : img.php * Author : freemouse * web : www.cnphp.info * email :freemouse1981@gmail.com * Date : 2010/12/27 * Usage: * <img src=img.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
/**********************************************
* 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设计模式 Composite (组合模式)
- php设计模式 State (状态模式)
- php设计模式 Bridge (桥接模式)
- php设计模式 Chain Of Responsibility (职责链模式)
- php设计模式 FlyWeight (享元模式)
- php设计模式 Mediator (中介者模式)
- php设计模式 Prototype (原型模式)代码
- PHP如何解决网站大流量与高并发的问题
- session在PHP大型web应用中的使用
- php URL跳转代码 减少外链
- php session安全问题分析
- 使用PHP实现二分查找算法代码分享
- PHP求最大子序列和的算法实现
- php中转义mysql语句的实现代码
- 把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。
- php中对2个数组相加的函数
- php判断输入不超过mysql的varchar字段的长度范围
- PHP array操作10个小技巧分享
- php 缩略图实现函数代码