PHP头条
热点:

php 随机显示图片的多种方法


<?
$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.zhutiai.com * 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> 

www.phpzy.comtrue/php/34155.htmlTechArticlephp 随机显示图片的多种方法 ? $handle = opendir('./'); //当前目录 while (false !== ($file = readdir($handle))) { //遍历该php教程文件所在目录 list($filesname,$kzm)=explode(.,$file);//获取扩展名 if ($kzm==gif or...

相关文章

    暂无相关文章

PHP之友评论

今天推荐