<?
$ 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 qualified file name to an array
}
}
}
$ Suiji = array_rand ($ array); // use the array_rand function to randomly extract a unit from the array.
?>
">
Instance 2
<? Php
/*************************************** *******
* Filename: img. php
* Author: freemouse
* Web: www.zhutiai.com * mail: freemouse1981@gmail.com
* Date: 2010/12/27
* Usage:
*
*
**************************************** *******/
If ($ _ GET ['folder']) {
$ Folder = $ _ GET ['folder'];
} Else {
$ Folder = '/images /';
}
// The location where the image file is 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 special webpage effect LANGUAGE)
<?
$ Arrayall = file ("tp.txt" when reading the content of tp.txt to the array
$ Arrays = count ($ arrayall );
If ($ arrays = 1) {// because rand (0, 0) is wrong
$ Selectrand = 0;
} Else {
Srand (double) microtime () * 1000000); // sets the Random Seed.
$ Selectrand = rand (0, $ arrays-1 );
}
$ Exstr = explode (chr (9), $ arrayall [$ selectrand]); // randomly retrieve and divide all
?>
Document. write ('<a href = "<? Echo $ exstr [1];?> "Target =" new "> "Width =" 200 "height =" 50 "alt =" <? Echo $ exstr [0];?> "> </A> ');
HTML file
<Html>
<Body>
<Script language = 'javascript 'src = 'readrand. php'>
</Script>
</Body>
</Html>
(You can place scripty to the location you need, and add the setTimeout () function to implement timed refresh)
Random ad code
<? Php
######## Random ad display ##########
Function myads (){
$ Dir = "ads"; # Set the directory for storing records
// $ Dir = "ads"; # Set the directory for storing records
$ Ads = "$ dir/ads.txt"; # Set the ad code file
$ Log = "$ dir/ads. log"; # Set the ip record file
$ Ads_lines = file ($ ads );
$ Lines = count ($ ads_lines); # Total number of file lines
#### Total number of ad reads $ ads_count and display times to 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];
}
}
#### Determine the random display sequence number $ display_rand #####
Srand (double) microtime () * 1000000 );
$ Display_rand = rand (1, $ display_count );
### Determine the ad 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 an advertisement ########
$ Num = 0;
$ Flag = 0;
For ($ I = 0; $ I <$ lines; $ I ++ ){
If ((! Strcmp (substr ($ ads_lines [$ I],), "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 ad display times #########
$ Fp = fopen ($ log, "");
Fputs ($ fp, date ("Y-m-d H: I: s "). getenv ("REMOTE_ADDR "). "=> ". $ ads_num. "n ");
Fclose ($ fp );
}
?>
Ad code file ads.txt
Reference content is as follows:
######## Each advertisement code is separated by '#'. display indicates the display weight. The larger the value is, the more times it is displayed ######
Display = 10
<A href = "ad 1 connection address">
</a>
################################
Display = 10
<A href = "Ad 2 connection address" target = _ blank>
</a>