Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:11:55+00:00 2026-05-31T07:11:55+00:00

i have been busting my head trying to figure this out. I found this

  • 0

i have been busting my head trying to figure this out.

I found this snippet of code that works well , taken from the official php website. But if i place one image and one pdf in the folder it dosent show up. The photo.

I need a very simple way to scan a directroy, check if its jpg and then display just one photo.

Please tell me how i can change the following code or if i need to write something different for my needs.

<?php // no access
.............

// globals 

.............

// make article title lower case

$str = strtolower($articleTitle); 

$files= scandir("images/gallery/".$str."/");
$photos = array();
        for ($x=0; $x<count($files); $x++){
        $files[$x]="images/gallery/".$str."/".$files[$x];
                if (is_dir($files[$x])){
                $thisfolder=scandir($files[$x]);
                for ($f=0; $f<count($thisfolder); $f++)
                if (strpos(strtolower($thisfolder[$f]), ".jpg"))
                $photos[]=$files[$x]."/".$thisfolder[$f];
                }
        }
$rand=rand(0, count($photos)); ?>
<img src="includes/crop.php?src=<?php echo $photos[$rand];?>&h=115&w=650&q=90" title="<?php echo $photos[$rand];?>" />

Thanks in advance again 🙂
john

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:11:56+00:00Added an answer on May 31, 2026 at 7:11 am

    Change the line

    $rand=rand(0, count($photos))
    

    to

    $rand=rand(0, count($photos)-1)
    

    Do you use rand to pick a random image but you have to limit the values in the interval
    [0, count($photos)-1] otherwise an index overflow can occurs. The bug afflicts your code but it is the case of a directory containings just an image that the problem is quite evident.

    count($photo) equals to 1 and the array contains just an element with index 0.

    $rand=rand(0, count($photos)) => $rand=rand(0,1) => $rand can assume two different values: 0 or 1. In the first case all work as intended. In the latter the bug will spring out.

    References:
    http://www.php.net/rand

    Addendum

    In response to the comment: The code is quite efficient in case of an empty directory as the execution will skip the external for statement. You can use an if to skip all the code but the efficiency gain will be negligible.

    .............
    
    $str = strtolower($articleTitle); 
    
    $files= scandir("images/gallery/".$str."/");
    if (count($files)) {
        $photos = array();
            for ($x=0; $x<count($files); $x++){
            $files[$x]="images/gallery/".$str."/".$files[$x];
                if (is_dir($files[$x])){
                    $thisfolder=scandir($files[$x]);
                    for ($f=0; $f<count($thisfolder); $f++)
                    if (strpos(strtolower($thisfolder[$f]), ".jpg"))
                        $photos[]=$files[$x]."/".$thisfolder[$f];
                }
            }
        $rand=rand(0, count($photos)-1);
        echo '<img src="includes/crop.php?src=' . $photos[$rand] 
           . '&h=115&w=650&q=90" title="'$photos[$rand]" />';
    }
    

    I would use a different approach though, something like this:

    $dirName = strtolower($articleTitle);
    $oldDir = cwd();
    chdir('images/gallery/'.$dirName);
    $photos = glob('*.jpg');
    $foreach(glob('*',GLOB_ONLYDIR) as $subdir) {
        $photos = array_merge($photos, glob($subdir.'/*.jpg'));
    }
    if (count($photos)) {
        $rand=rand(0, count($photos)-1);
        echo '<img src="includes/crop.php?src=' . $photos[$rand] 
             . '&h=115&w=650&q=90" title="'$photos[$rand]" />';
    }
    chdir($oldDir);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure out what is wrong with my test. First,
I have been trying for a while now, but I can't figure out how
I have been trying this for the whole day and I managed to make
I've been busting my head on this, and not so sure what to do.
Have been trying to fix this problem I have here. Basically I have a
Have been trying the whole day long and googled the **** out of the
Been busting my head on this one - time to give it up to
have been working and wearing out my fingers doing google searches with this one:
Have been pulling out my hair trying to find out why my sessions are
I have several old sites that have just been taken offline by my hosting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.