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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:57:18+00:00 2026-05-26T21:57:18+00:00

I need to get the files in a directory in an array. Only files

  • 0
  1. I need to get the files in a directory in an array.
    • Only files that end in .markdown
  2. The files should be sorted by date created

Efficiency is not a huge deal, but I always like to make things as efficient as possible. There will be multiple directories with up to a couple hundred files in each.

I know I use the scandir function to get the files into an array, but I’m not sure where to go from there. I could throw it in a loop to check if the files end in .markdown, but is there some sort of regex I could pass in to the scan?

Thanks

  • 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-26T21:57:19+00:00Added an answer on May 26, 2026 at 9:57 pm

    You can use glob() to get an array of filenames then use usort() and filectime() to order them by their creation dates:

    $files = glob('*.markdown');
    usort($files, function($file_1, $file_2)
    {
        $file_1 = filectime($file_1);
        $file_2 = filectime($file_2);
        if($file_1 == $file_2)
        {
            return 0;
        }
        return $file_1 < $file_2 ? 1 : -1;
    });
    

    For PHP versions lower than 5.3:

    function sortByCreationTime($file_1, $file_2)
    {
        $file_1 = filectime($file_1);
        $file_2 = filectime($file_2);
        if($file_1 == $file_2)
        {
            return 0;
        }
        return $file_1 < $file_2 ? 1 : -1;
    }
    
    $files = glob('*.markdown');
    usort($files, 'sortByCreationTime');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get a count of the number of files in a directory.
Possible Duplicate: Get all files from VSS for a given date? I need to
I use the following to get a list of project files that need to
i need to get all files in a directory, including all sub directories and
I need to get the size of files. These files range in size from
I need to get data from flat files to DB using SSIS . The
I have a little doubt. I need to get a list of the files
With ANTLR, I get some java class files after compilation. And I need to
I have an array that lists folders in a directory. Until now, I've been
I have created a piece of code that checks files for a user-submitted string

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.