WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : This script will read all images from a folder and read the files into an array. It uses rand() to get a random number. It will display a random image from the image folder given.
Categories : PHP, Arrays, Graphics, Filesystem Click here to Update Your Picture
Christian Haensel
Date : Dec 11th 2007
Grade : 5 of 5 (graded 1 times)
Viewed : 6915
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Christian Haensel
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 




<?php
/*
Random image display

This script will read all images from a folder and read the files into an array.
It uses rand() to get a random number. It will display a random image from the image folder given.

The check for whether it is an image or not is made by checking the file's extension.

Programmed by Christian Haensel on Tuesday, Dec. 12, 2007.
Visit my website at http://www.chftp.com. Email me at haensel@chftp.com

This script may be redistributed, altered and whatnot. Please, if you like it and/or find it useful, link back to me.

Chris

*/

// Point to the folder containing the images. Relative path without trailing slash!
$imgfolder    =    "images/mini";


// Checks if the file is an image. If not, it won't be added to the image list
function is_image($filename){
   
$filename    =    strtolower($filename) ;
   
$ext        =    split("[/\\.]", $filename) ;
   
$n            =    count($ext)-1;
   
$ext        =    $ext[$n];

   
// Here you can list the extensions you want to be allowed
   
if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG" || $ext == "gif" || $ext == "GIF" || $ext == "png" || $ext == "PNG") {
        return
true;
    } else {
        return
false;
    }
}

// Read the image folder into an array
$dir    =    opendir($imgfolder);
$array    =    array();
// Run through the folder
while($file = readdir($dir)) {
   
// Checks if the file is an image. Check the is_image() function a but up in this file
   
if(is_image($file)) {
       
// It's an image!! Put it into the array
       
array_push($array, $file);
    }
}

// Count the amount of images in the array
$count    =    (count($array)-1);

// Generate a random number
$rand    =    rand(0,$count);

// Select an image from the array based on the random number
$img    =    $array[$rand];

// Output the image
echo '<img src="'.$imgfolder.'/'.$img.'" border="1">';
?>



How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP
Simple way of scaling any image to fit either given width or height.
Categories : PHP, Graphics, Arrays
Selecting a random line from a text file
Categories : PHP, Filesystem, Arrays, Beginner Guides
Random Image Display
Categories : PHP, Filesystem, Graphics, HTML and PHP
Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
Read DPI value from image with PHP
Categories : PHP, Graphics, Filesystem
Simple image counter
Categories : PHP, Graphics, Filesystem, Beginner Guides
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
This is a simple photo gallery that reads the image files from multiple directories, and generates a web page styled with CSS1. It opens single auto window to view and print a given image.
Categories : Graphics, Filesystem, PHP, Complete Programs
This program implements hot link prevention in php. It is useful for webmasters who do not have access to the server at a level where they can control hot linking can still supply some type of hot link prevention for thier site by using php.
Categories : PHP, Filesystem, Graphics, Content Management
A function which places the path and name of all subdirectories into an array
Categories : PHP, Filesystem, Arrays, Directories
Easy slideshow with php. Just place this one file into a subdirectory where jpg files reside. An instant slideshow is yours. MS explorer only.
Categories : PHP, Filesystem, Graphics
 agnes swart wrote :1819
hey this is a very nice code, as it just checks how many 
images are in the folder.

would it be possible to change the random image into a 
string of images? I mean, to start with image 1, and make 
a button to go to the next.

it would be an awesome slideshow, because you wouldn't 
need to name your files or add how many there are.

the problem of reaching the end could be fixed maybe by 
restarting at no. 1 or even 0.

would it be possible? thanks!