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