兩組PHP代碼的隨機圖像顯示程序

1:PHP隨機顯示圖片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
/*
*   Author:   http://www.yox.net.ru
*/


$url = "./images"; //注意:圖片文件夾路徑,不可含有'/';
$files = array();
if(false !== ($handle = @opendir($url)))
{
  while($file = readdir($handle))
{
if(($file !== ".")&&($file !== ".."))
{
  if((substr($file,-3) == "gif") || (substr($file,-3) == "jpg") || (substr($file,-3) == "png"))
   $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");

elseif(substr($files[$random],-3) == "png")
   header("Content-type:image/png");
   readfile("$url/$files[$random]"); //讀文件,顯示圖片;
}else
  echo "<b>圖片目錄<font color=red>$url</font>不存在!請重新設置!</b>";
?>

2:PHP隨機圖片
創建名為1到15的gif圖片,隨機調用顯示.(注意,文件名為1.gif到15.gif之間)
可用于論壇頭像,嘿嘿…

1
<?readfile(rand(1,15).".gif");?>

收藏自:http://www.yox.net.ru/random_pic_bbs/