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 6129207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:55+00:00 2026-05-23T16:42:55+00:00

<?php /* settings */ $image_dir = ‘gallery/’; $per_column = 3; $count=0; if ($handle =

  • 0
<?php        
    /* settings */  
    $image_dir = 'gallery/';  
    $per_column = 3;  
    $count=0;
      if ($handle = opendir($image_dir)) {  
        while (false !== ($file = readdir($handle)))  
        {  
            if ($file != '.' && $file != '..')  
            {  
                if(strstr($file,'-thumb'))  
                {  
                    $files[] = $file;  
                }  
            }  
        }  
        closedir($handle);  
    }  
    if(count($files))  
    {  
        foreach($files as $file)  
        {   
            $count++;  
            echo '<a class="thumbnail" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"><img src="',$image_dir,$file,'" width="100" height="100" /></a>';  
            if($count % $per_column == 0) { echo '<div class="clear"></div>'; }  
        }  
    }  
    else  
    {  
        echo '<p>There are no images in this gallery.</p>';  
    }  

?>  

How can I add captions to each of the images?
Thank you very much for the answer!

  • 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-23T16:42:56+00:00Added an answer on May 23, 2026 at 4:42 pm

    Your code is traversing the files within the specified directory looking for files containing ...-thumb... in the filename, appending them to an array, and then looping over the array to generate HTML for displaying the thumbnail image gallery.

    Adding additional information to something like this, given the very limited code you’ve provided, can be done in any number of ways

    1. You could implement a database with a column for each filename and a
      related column containing captions/descriptions. This might be more
      trouble than it’s worth depending on what you’re trying to achieve.
    2. You could use a flat file which you could parse line by line (instead of traversing the
      folder for ...-thumb... images)
      , containing some format like

      file1-thumb.png|some caption here
      file2-thumb.png|some caption here
      file3-thumb.png|some caption here
      ...
      
    3. You could includ a small caption in the filename itself, and parse/format the caption from the filenames. This would probably be the quickest route, but most limiting in terms of flexibility in the length/characters allowed for the captions.

      - file1-thumb--some_caption_here.png
      - file2-thumb--some_caption_here.png
      - file3-thumb--some_caption_here.png
      

    To actually add the caption to the generated HTML, you can use a title attribute as @rockerest suggested, however I’d personally add such a caption to to the image itself, as that is what the caption is describing (not the link)

    <img src="..." title="..." ... />
    

    UPDATE

    To answer your comment (this provides better formatting for code), Let’s say we have a file with name file1-thumb--some_description_here.jpg, you can parse and format the caption with preg_replace

    $filename = 'file1-thumb--some_description_here.jpg';
    $caption = preg_replace(array('/^.+-thumb--/', '/\.(jpg|jpeg|gif|png|bmp)$/', '/_/'), array('','',' '), $filename);
    

    $caption is now some description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a settings.php file with parametric variables and a functions.php with well, some
What are the best file permission settings for PHP scripts? Any suggestions on ways
Currently all of my script's settings are located in a PHP file which I
<?php include ../music/php/logic/core.php; include ../music/php/logic/settings.php; include ../music/php/logic/music.php; $top = At world's end; // create
I am trying to read some settings from php.ini using zend. The API that
I am writing a PHP application that will have the ability to edit settings
I have a large PHP application. After I changed some settings I get a
I have a PHP script that creates a thumbnail and lists an image gallery.
Currently the php framework I'm dealing with has a simple conf file with define
I have a PHP script that initialises an image gallery. It loops through all

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.