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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:06:27+00:00 2026-05-26T20:06:27+00:00

I’ve written a PHP script that iterates through a given folder, extracts all the

  • 0

I’ve written a PHP script that iterates through a given folder, extracts all the images from there and displays them on an HTML page (as tags). The size of the page is about 14 KB, but it takes the page almost 15 seconds.

Here’s the code:

function displayGallery( $gallery, $rel, $first_image ) {
    $prefix = "http://www.example.com/";
    $path_to_gallery = "gallery_albums/" . $gallery . "/";
    $handler = opendir( $path_to_gallery ); //opens directory
    while ( ( $file = readdir( $handler ) ) !== false ) { 
        if ( strcmp( $file, "." ) != 0 && strcmp( $file, ".." ) !=0 ) { 
            //check for "." and ".." files
            if ( isImage( $prefix . $path_to_gallery . $file ) ) {
                echo '<a href="' . $path_to_gallery . $file . '"></a>';
            } 
        }
    }
    closedir( $handler ); //closes directory
}

function isImage($image_file) {
  if (getimagesize($image_file)!==false) {
    return true;
  } else {
    return false;
  }
}

I looked at other posts, but most of them deal with SQL queries, and that’s not my case.

Any suggestions how to optimize this?

  • 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-26T20:06:27+00:00Added an answer on May 26, 2026 at 8:06 pm

    My assumption is that isImage is the problem. I’ve never seen it before. Why not just check for particular file extensions? That’s pretty quick.
    Update: You might also try switching to use exif_imagetype() which is likely faster than getimagesize() Putting that check into the top function is also going to be faster. Neither of those functions was meant to be done over a web connection – avoid that altogether. Best to stick with the file extension.

    Do you not already have access to the files directly? Every time you look something up over the web, it’s going to take a while – you need to wait for the entire file to download. Look up the files directly on your system.

    Use scandir to get all the filenames at once into an array and walk through them. That will likely speed things up as I assume there won’t be a back and forth to get things individually.

    Instead of doing strcmp for . and .. just do $file != '.' && $file != '..'

    Also, the speed is going to depend on the number of files being returned, if there are a lot it’s going to be slow. The OS can slow down with too many files in a directory as well. You’re looping over all files and directories, not just images so that’s the number that counts, not just the images.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.