icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.

» Surpass Web Hosting Forums » Discussions » PHP, MySQL » Random Image

PHP, MySQL General PHP questions. Or go to our PHPsuexec Forum >>

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
Old April 8th, 2005, 4:09 AM   #1 (permalink)
Surpass Fan
Comfy Contributor
 
Joined in May 2004
142 posts
Gave thanks: 0
Thanked 0 times
Random Image

Does anyone know a code to have a random image display in the directory?

Tidal Wave is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8th, 2005, 7:58 AM   #2 (permalink)
Surpass Fan
On a golden path...
 
ddouma's Avatar
 
Joined in Aug 2004
Lives in The Netherlands
Hosted on pass11, sh57
316 posts
Gave thanks: 0
Thanked 2 times
Already searched on hotscripts.com?
__________________
---
Best Regards,
D.L.H. Douma
ddouma is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8th, 2005, 9:13 AM   #3 (permalink)
ceo
Senior Member
Super #1
 
Joined in Jan 2005
1,546 posts
Gave thanks: 70
Thanked 33 times
http://photomatt.net/scripts/randomimage

[ moved | php question | ancyru ]

Last edited by Ancyru; April 8th, 2005 at 10:06 AM..
ceo is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8th, 2005, 10:06 AM   #4 (permalink)
Surpass Fan
Super #1
 
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
PHP Code:
<?php 
/// Set $dirname to your full path
$dirname $_SERVER['DOCUMENT_ROOT']."/image_folder/"// Include trailing slash
/// End config
$i 0;
$pics = array();
$dir opendir$dirname );
while( 
false != ( $file readdir$dir ) ) ){
    if( !
is_dir$file ) ){
        
$pics[$i] = $file;
    }
    
$i++;
}

$max count$pics ) - 1
$rand rand0$max ); 
echo( 
'<img src="'.$pics[$rand].'" alt="banner" />' ); 
?>
Should do the trick nicely for you...
__________________
- Evan Charlton | [site] | Server - SH58
Kickersny.com is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8th, 2005, 1:27 PM   #5 (permalink)
Surpass Fan
On a golden path...
 
Joined in Nov 2004
Lives in San Francisco
364 posts
Gave thanks: 0
Thanked 3 times
In case you have files in your image directory that aren't images:

PHP Code:
<?php
$dir 
''// Set this to your image directory relative to the script, or leave blank
foreach(glob($dir.'*') as $image) {
  switch (
strtolower(substr($image,-3))) {
  case 
'jpg':
  case 
'png':
  case 
'gif':
    
$images[] = $image;
    break;
  }
}
shuffle($images);
echo 
'<img src="'.$images[0].'" alt="A Random Image"/>';
?>
ghostcop is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 8th, 2005, 4:39 PM   #6 (permalink)
Surpass Fan
Super #1
 
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
ghostcop: what about *.tiff? or *.jpeg even? Unlikely, yet possible.
__________________
- Evan Charlton | [site] | Server - SH58
Kickersny.com is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old April 9th, 2005, 6:19 AM   #7 (permalink)
Surpass Fan
On a golden path...
 
Joined in Nov 2004
Lives in San Francisco
364 posts
Gave thanks: 0
Thanked 3 times
To add an extension to the php code I wrote, simply add a
PHP Code:
case 'ext'
above the case 'jpg': where ext is the extension of the file type. Removing any of the existing case statements will prevent that file type from being displayed.
ghostcop is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On