Jump to content

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


play_

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?
Link to comment
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
Link to comment
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.
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.