Multiple ways to randomly display images in php_PHP tutorial
Jul 13, 2016 am 10:46 AM<?
$handle = opendir('./'); //Current directory
while (false !== ($file = readdir($handle))) { //Traverse the directory where the php tutorial file is located
list($filesname,$kzm)=explode(".",$file);//Get the extension
if ($kzm=="gif" or $kzm=="jpg") { //File filtering
if (!is_dir('./'.$file)) { //Folder filtering
$array[]=$file;//Save the file names that meet the conditions into the array
}
}
}
$suiji=array_rand($array); //Use array_rand function to randomly extract a unit from the array
?>
<img src="<?=$array[$suiji]?>">
Example 2
<?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/';
}
//The location where image files are stored
$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 (This program actually generates a web page special effects language)
<?
$arrayall=file("tp.txt"); Read the contents of tp.txt into array
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//Set random number seed
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr(9),$arrayall[$selectrand]);//Randomly pick one out of all and split
?>
document.write('<a href="<? echo $exstr[1];?>" target="new"><img src="<? echo $exstr[2];?> ;" width="200" height="50" alt="<? echo $exstr[0];?>" ></a>');
HTML file
<html>
<body>
<script language='javascript' src='readrand.php'>
</script>
</body>
</html>
(You can put scripty where you need, and add the setTimeout() function to achieve regular refresh)
Random ad code
<?php
#########Random ad display##########
function myads(){
$dir="ads"; #Set the directory where records are stored
//$dir="ads"; #Set the directory where records are stored
$ads="$dir/ads.txt"; #Set advertising code file
$log ="$dir/ads.log"; #Set ip record file
$ads_lines=file($ads);
$lines=count($ads_lines);#Total number of lines in the file
####Read the total number of ads $ads_count and the number of displays into the array $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];
}
}
####Decided to randomly display the serial number $display_rand#####
srand((double)microtime()*1000000);
$display_rand = rand(1,$display_count);
###Determine the advertising serial number $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;}
}
#####Play ads########
$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;}
}
####Record the number of times ads are displayed#########
$fp=fopen($log,"a");
fputs($fp,date( "Y-m-d H:i:s " ).getenv("REMOTE_ADDR")."==>".$ads_num."n");
fclose($fp);
}
?>
Advertising code file ads.txt
The following is the quoted content:
########Each ad code is separated by '#', display is the display weighting number, the larger the number, the more times it will be displayed ######
display=10
<a href="Advertising 1 connection address">
<img src=/images/banner/webjxcomad1.gif" alt="Advertisement 1"> </a>
###############################
display=10
<a href="Advertising 2 connection address" target=_blank>
<img src=images/banner/webjxcomad2.gif" width="468" height="60" alt="Ad 2" border="0"></a>

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
