php中隨機(jī)顯示圖片的函數(shù)代碼
更新時(shí)間:2011年06月23日 22:28:36 作者:
一個(gè)隨機(jī)顯示圖片的函數(shù),可以將指定文件夾中存放的圖片隨機(jī)地顯示出來(lái),可以在很多地方使用
例如博客的展示窗
<?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]");
?>
復(fù)制代碼 代碼如下:
<?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]");
?>
相關(guān)文章
PHP框架Laravel插件Pagination實(shí)現(xiàn)自定義分頁(yè)
這篇文章主要為大家詳細(xì)介紹了PHP框架Laravel5.1插件Pagination實(shí)現(xiàn)自定義分頁(yè)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05總結(jié)PHP刪除字符串最后一個(gè)字符的三種方法
這篇文章為大家講解的是php實(shí)現(xiàn)如何去掉字符串的最后一個(gè)字符的多種方法,感興趣的同學(xué)參考下。下面來(lái)一起看看。2016-08-08Warning:?require():?open_basedir?restriction?in?effect,
在linux服務(wù)器部署thinkphp5的時(shí)候PHP報(bào)了Warning:?require():?open_basedir?restriction?in?effect這個(gè)錯(cuò)誤,是因?yàn)榫W(wǎng)站目錄配置錯(cuò)誤,PHP不能引入其授權(quán)目錄上級(jí)及其以上的文件。下面詳細(xì)講解如何處理這個(gè)問(wèn)題,需要的朋友可以參考下2022-11-11PHP回調(diào)函數(shù)與匿名函數(shù)實(shí)例詳解
這篇文章主要介紹了PHP回調(diào)函數(shù)與匿名函數(shù),結(jié)合實(shí)例形式分析了php回調(diào)函數(shù)與匿名函數(shù)的具體功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08基于wordpress主題制作的具體實(shí)現(xiàn)步驟
本篇文章是對(duì)wordpress主題制作的具體實(shí)現(xiàn)步驟進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05