php 隨機顯示圖片的多種方法

來源:互聯網
上載者:User

<?
$handle = opendir('./'); //目前的目錄
while (false !== ($file = readdir($handle))) { //遍曆該php教程檔案所在目錄
list($filesname,$kzm)=explode(".",$file);//擷取副檔名
if ($kzm=="gif" or $kzm=="jpg") { //檔案過濾
if (!is_dir('./'.$file)) {  //檔案夾過濾
$array[]=$file;//把合格檔案名稱存入數組
}
}
}
$suiji=array_rand($array); //使用array_rand函數從數組中隨機抽出一個單元
?>
<img src="<?=$array[$suiji]?>">

執行個體二

<?php
/**********************************************
* Filename : img.php
* Author : freemouse
* web : www.111cn.net * 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]");
?>


readrand.php(此程式實際上是產生一句網頁特效語言)
<?
$arrayall=file("tp.txt");讀出tp.txt內容到數組
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//設定隨機數種子
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr(9),$arrayall[$selectrand]);//從全部中隨機取出一個並分割
?>
document.write('<a href="<? echo $exstr[1];?>" target="new"><img src="<? echo $exstr[2];?>" width="200" height="50" alt="<? echo $exstr[0];?>" ></a>');


HTML檔案
<html>
<body>
<script language='javascript' src='readrand.php'>
</script>
</body>
</html>


(你可以把scripty放到你需要的位置,並可以加入setTimeout()函數以實現定時重新整理)

隨機廣告代碼

<?php
  #########隨機廣告顯示##########
  function myads(){
  $dir="ads"; #設定存放記錄的目錄
  //$dir="ads"; #設定存放記錄的目錄
  $ads="$dir/ads.txt"; #設定廣告代碼檔案
  $log ="$dir/ads.log"; #設定ip記錄檔案
  
  $ads_lines=file($ads);
  $lines=count($ads_lines);#檔案總行數
  
  ####讀出廣告總數$ads_count和顯示次數到數組$display_array########
  $ads_count=0;
  $display_count=0;
  for ($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){
   $ads_count+=1;
   $display_array[$ads_count]=substr($ads_lines[$i],8);
   $display_count+=$display_array[$ads_count];
   }
  }
  ####決定隨機顯示序號$display_rand#####
  srand((double)microtime()*1000000);
  $display_rand = rand(1,$display_count);
  
  ###決定廣告序號$ads_num######
  $pricount=0;
  $ads_num=1;
  for($i=1; $i<=$ads_count; $i++) {
   $pricount += $display_array[$i];
   if ($display_rand<=$pricount) {$ads_num=$i;break;}
  }
  
  #####播放廣告########
  $num=0;
  $flag=0;
  
  for($i=0;$i<$lines;$i++){
   if((!strcmp(substr($ads_lines[$i],0,7),"display"))){$num++;}
   if(($num==$ads_num)and($flag==0)){$flag=1;continue;}
   if(($flag==1)and strcmp($ads_lines[$i][0],"#")){echo $ads_lines[$i];continue;}
   if(($flag==1)and(!(strcmp($ads_lines[$i][0],"#")))){break;}
  }
  ####紀錄廣告顯示次數#########
  $fp=fopen($log,"a");
  fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
  fclose($fp);
  }
  ?>


廣告代碼檔案ads.txt

以下為引用的內容:
  ########每個廣告代碼之間用'#'隔開,display為顯示加權數,越大顯示次數越多######
  display=10   
  <a href="廣告1串連地址">
  <img src=/images/banner/webjxcomad1.gif" alt="廣告1"> </a>
  ################################
  display=10   
  <a href="廣告2串連地址" target=_blank>
  <img src=images/banner/webjxcomad2.gif" width="468" height="60" alt="廣告2" border="0"></a> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.