Re: Random Images

From: Date: Thu, 06 Feb 2003 07:04:09 +0000
Subject: Re: Random Images
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-134562@lists.php.net to get a copy of this message
This one time, at band camp, V Dub <rw@xend.net> wrote: > Tom! > > Here's one way: > or you could do something like this... Simply set the image directory and fire it up <?php // just so we know it is broken error_reporting(E_ALL); // set the images directory $imgDir = './images'; // open the image dir $dir = opendir("$imgDir"); // read the files in the image dir while (false !== ($file = readdir($dir))) { // make sure only image files are selected $t = @getimagesize($imgDir.'/'.$file); if ($file != "." && $file != ".." && $t['2'] < 15 && !is_dir($imgDir.'/'.$file)) { // put the file names in an array $file_array[] = $file; } } // close the dir closedir($dir); // make sure there are files in the array :/ if(!isset($file_array)) { echo 'No image files found'; } else { // shuffle the array shuffle($file_array); // pick one off the top $randImg = $file_array['0']; // get the file info $size = getimagesize ($imgDir.'/'.$randImg); // open the file for reading $fp=@fopen($imgDir.'/'.$randImg, "rb"); // if everything is cool... if ($size && $fp) { // set the headers header("Content-type: {$size['mime']}"); // show the image fpassthru($fp); // bail out exit; } else { // show an error echo 'Sorry, No images available in '. $imgDir; } } ?> or something like that, someone is bound to show some mistakes in here but it works Kevin -- ______ (_____ \ _____) ) ____ ____ ____ ____ | ____/ / _ ) / _ | / ___) / _ ) | | ( (/ / ( ( | |( (___ ( (/ / |_| \____) \_||_| \____) \____) Kevin Waterson Port Macquarie, Australia

« previous php.general (#134562) next »