Home > Backend Development > PHP Tutorial > Multiple ways to randomly display images in php_PHP tutorial

Multiple ways to randomly display images in php_PHP tutorial

WBOY
Release: 2016-07-13 10:46:07
Original
795 people have browsed it

$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
?>

Example 2

/**********************************************
* Filename : img.php
* Author : freemouse
* web : www.zhutiai.com * email :freemouse1981@gmail.com
* Date : 2010/12/27
* Usage:
*
*
***********************************************/
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('<? echo $exstr[0];?>');


HTML file





(You can put scripty where you need, and add the setTimeout() function to achieve regular refresh)

Random ad code

#########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

Advertisement 1

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

Ad 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632980.htmlTechArticle? $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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template