Jump to content

Archived

This topic is now archived and is closed to further replies.

play_

Can someone tell me what's wrong with this random image selector i made?

Recommended Posts

Ok ill be honest. It's been quite a bit since ive done php and now im redoing my site. Instead of using the old scrips ive made, im making everything from scratch, to refresh the memory.

Im making a script that pics a random image out of a directory. It looks like this:

[code]
        <?php //Random image loader.
        $path = './images/logos/';
        $dir = opendir($path) or die('Could not open directory for main image selection.');
        $i = 0;
        
        // Loops through directory and append filenames to the array 'file'.
        while( ($file[$i] = readdir($dir)) ) {
            $i++;
        }

        // pick a random item (image name) out of the 'file' array :: $file[1] or $file[2], etc.
        $image = array_rand($file);

        

        if( ($file[$image] != '.') && ($file[$image] != '') && ($file[$image] != 'Thumbs.db') && ($file[$image] != '..') && ($file != ' ') ) {
                        $all = $path.$file[$image];
            echo '<img src="'. $all .'" />';
        } else {
            echo $file[$image];
        }
[/code]

As you can see, in the 'else' statement, it prints the name of the image/file (i put it there because sometimes the image doesnt show up). And so, when the image doesn't show up, it prints either 'Thumbs.db' or '.' or '..'.

Why is that happening, when i have that long ass 'if' statement there telling the script to not print those files?

Share this post


Link to post
Share on other sites
Guest askjames01
i suggest you use elseif instead of else only...
because sometimes if else alone doesn't work as you expected.
try changing it if this doesn't work then just pm again.

so

[code]    if( ($file[$image] != '.') && ($file[$image] != '') && ($file[$image] != 'Thumbs.db') && ($file[$image] != '..') && ($file != ' ') ) {
                        $all = $path.$file[$image];
      echo '<img src="'. $all .'" />';
    } elseif('your condition here') {
      echo $file[$image];
   }[/code]

just don't gorget the new condition inside elseif.
this might solve your problem

Share this post


Link to post
Share on other sites
Hey James. It just so happens i figured a way to make it work (right after you posted too....i waited a few minutes and no one replied, so i figured everyone was asleep).

Here is what i came up with:

[code]
                $path = './images/logos/';
        $dir = opendir($path) or die('Could not open directory for main image selection.');
        $images = array();
        while ( !(($file = readdir($dir)) === false)) {
            if ( ($file != 'Thumbs.db') && ($file != '.') && ($file != '..') ){
                array_push($images, $file);
                
            }
        }
        shuffle($images);
        print $images[0];        [/code]

It's pretty small and so far works fine. If you can see something that might not be well written, let me know though.

But, i still dont understand why in that first script i posted, php was selecting 'Thumbs.db' and '.' and '..' from the directory even though i had an IF statement telling it to skip those files.

Share this post


Link to post
Share on other sites
well umm.. i tested your code (from original post) on my server. the only thing i changed was change the $path to ./images/ instead of ./images/logos/ (to suit my own directories) and it worked just fine

p.s.- maybe you should try using || instead of && (or instead of and) in your if statement

Share this post


Link to post
Share on other sites
[!--quoteo(post=371146:date=May 4 2006, 12:25 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 4 2006, 12:25 AM) [snapback]371146[/snapback][/div][div class=\'quotemain\'][!--quotec--]
well umm.. i tested your code (from original post) on my server. the only thing i changed was change the $path to ./images/ instead of ./images/logos/ (to suit my own directories) and it worked just fine

p.s.- maybe you should try using || instead of && (or instead of and) in your if statement
[/quote]

Oh.
If you upload a directory full of images to your server, it till upload 'Thumbs.db' with it.
Keep clicking refresh on your browser, and you'll see that sometimes the image won't show up.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.