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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:14+00:00 2026-05-28T01:49:14+00:00

Below is a file iterarot unction I have working on that iterates over folders

  • 0

Below is a file iterarot unction I have working on that iterates over folders and gathers all images, it saves each image’s file path to a $files array and then later on the code has to iterate over that $files array

Right now the code I am working on actually sens a file through imagemagick 3 times ( I didn’t write the original code) so I am trying to improve the whole code, So far I have cut that down to 2 times and when this is finished it will be only 1 time for each image.

Basically the current code runs an image through imagemagick to see it it is an image or not, later on it does it again but this time to get the image extension, and then the 3rd time was to make sure it was an image again (I know pretty crazy that people write code that bad)

So to reduce it to 1 time, when the $files array runs an image file through imagemagick the first time to make sure it is an image, I now have that function returning the file extension instead of a simple true/false. So when the filepath is saved to the array I would like to save the returned file extension to the array with that particular file path so that I can access it later without running imagemagick again.

So here is the function that cycle thorough folders grabbing all image files and then sends them to imagemagick. It then saves the filepath to the array, can someone show me how to save the file extension to this array and then how to access it in the code below the function

function find_recursive_images($path)
{
    $directory = new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS);
    $iterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY);
    $exntensions = array("png", "jpg", "jpeg", "gif", "bmp");

    foreach ($iterator as $fileinfo) {
        if (in_array($fileinfo->getExtension(), $exntensions)) {
            //This gets the filetype of the image from ImageMagick
            $img = self::get_type($fileinfo->getPathname());
            if ($img) {
                //This sets the full file path to the $files array
                $files[] = $fileinfo->getPathname();

                // I tried something like this but not correct
                $files[][] = $img;
            }
        }
    }
    return $files;
}

I then access the array like this right now…

foreach ($files as $file)
{    
     // ie E:\Server\_ImageOptimize\img\testfiles\css3-please.png
     $source_file = $file;  

    // $file also needs to return a filetype that is got when it ran thorugh imagemagick

}
  • 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-28T01:49:15+00:00Added an answer on May 28, 2026 at 1:49 am

    How about you use an associative array for each image? try this:
    To build your array:
    $files[] = array(location => $fileinfo->getPathname(), type => $img);

    To loop through your aray:

    foreach ($files as $file) {    
         $source_file = $file->location;  
         $extension  = $file->type;
    
         //do some stuff using your info
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple statement below: if(file_exists('/images/alum/'.$profile['pic'])) echo '/images/alum/'.$profile['pic']; else echo '/images/user_default.jpg'; The file
The below file contains all the javascript for a page. Performance is the highest
I have written the batch file below, in order to automate the process of
I have a data structure which is as given below: class File { public
I have a file in below format. DATE Time, v1,v2,v3 05:33:25,n1,n2,n3 05:34:25,n4,n5,n5 05:35:24,n6,n7,n8 and
I have bat file like below with name myBat.bat 1) @echo off set CLASSPATH=%CLASSPATH%;C:\Documents
I have a long xml file the content of the file are below: <?xml
I am working with the XML file string below and I've tried a number
I have created the below batch file but no success. @ECHO OFF @ECHO Backup
I have a Perl script to convert the XML file below into a hash:

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.