Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

[web] PHP Random Screenshot Code

This topic is 3318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get the contents of a directory and then put all the file names with jpg extension to an array.Then I try to pick a random filename from the array and disply it.However the array is empty after the while loop and I can't get anything to display.If I echo from the array inside the while loop I get the file names [oh]
<? 
$arrayNames = array();
$fileCount=0;
$filePath=$PATH.'darkage/screenshots/';
$dir = opendir($filePath); # Open the path

while ($file = readdir($dir)) 
{ 
  if (eregi("\.jpg",$file)) 
{ 
    $arrayNames[fileCount] = $file;
    $fileCount++;
  }
}
$random = rand(0,$fileCount);
$imgSrc=$arrayNames[$random];
echo $arrayNames[0]; //doesn't display anything
echo $imgSrc; //doesn't display anything
?>

<a href="darkage/screenshots/<? echo $imgSrc; ?>">
<img width=150 src="darkage/screenshots/<? echo $imgSrc;?>">
</a>



Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Black Knight


A shortened version of your code:

<?php

$globbed = $PATH.'darkage/screenshorts/*.jpg';
$files = glob($globbed);
$display = $files[rand(0,count($files))];
$uri = '/darkage/screenshots'.basename($display);

?><a href="<?=$uri?>"><img width=150 src="<?=$uri?>"/></a>



Share this post


Link to post
Share on other sites
Wow,didn't know there was a handy function like glob().Thanks for the info.
I just added "/" at the end, or it becomes /darkage/screenshotsgame011.jpg etc.


<?
$globbed = $PATH.'darkage/screenshots/*.jpg';
$files = glob($globbed);
$display = $files[rand(0,count($files))];
$uri = '/darkage/screenshots/'.basename($display);

?>
<a href="<?=$uri?>"><img width=150 src="<?=$uri?>"/></a>



Share this post


Link to post
Share on other sites
Note that there is a small but significant difference between glob and readdir in this case. glob is case sensitive. That means it won't find your files if the extension is ".JPG" rather than ".jpg". Your other code takes that into account by using eregi. It might not be an issue, but if it is, change the glob lines to:

$globbed = $PATH.'darkage/screenshots/*.{J,j}{P,p}{G,g}';
$files = glob($globbed, GLOB_BRACE);

Share this post


Link to post
Share on other sites
Also, instead of this:


$display = $files[rand(0,count($files))];


you can use:


$display = $files[array_rand($files)];


[Edited by - Sander on May 19, 2009 3:13:52 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Sander
$display = array_rand($files);
If I remember correctly, that should be $files[array_rand($files)].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!