欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-19 08:42:52
...
$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函数从数组中随机抽出一个单元
?>
php 随机显示图片的多种方法_PHP教程

实例二

/**********************************************
* Filename : img.php
* Author : freemouse
* web : www.zhutiai.com * email :freemouse1981@gmail.com
* Date : 2010/12/27
* Usage:
* php 随机显示图片的多种方法_PHP教程
* php 随机显示图片的多种方法_PHP教程
***********************************************/
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('php 随机显示图片的多种方法_PHP教程');


HTML文件






(你可以把scripty放到你需要的位置,并可以加入setTimeout()函数以实现定时刷新)

随机广告代码

#########随机广告显示##########
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 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 $pricount += $display_array[$i];
if ($display_rand }

#####播放广告########
$num=0;
$flag=0;

for($i=0;$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

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

################################
display=10

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632980.htmlTechArticle? $handle = opendir('./'); //当前目录 while (false !== ($file = readdir($handle))) { //遍历该php教程文件所在目录 list($filesname,$kzm)=explode(.,$file);//获取扩展名...