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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:04:50+00:00 2026-06-02T00:04:50+00:00

Background : I have a anonymous login ftp server, ftp_nlist only list files alphabetically,

  • 0

Background :
I have a anonymous login ftp server, ftp_nlist only list files alphabetically, I would like to get the list of files on the basis of last modified date, recent first. I tried ftp_exec($conn, “ls -t”) but I am presented with Permission Denied error, not sure why itsn’t working. Well I am working with php-cli and the number of files are in thousands, I just want to work with recent files. Getting the raw list, and finding the date part of the array elements might help, but I hope there is easy way out. When I login via terminal command ls -t works just fine. So wondering why ftp_exec is not working.

Seeking an easy suggestion.

Thanks in advance.

  • 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-06-02T00:04:52+00:00Added an answer on June 2, 2026 at 12:04 am

    If you only want to sort the files by last modified date, you can use

    ftp_nlist($conn, '-t .');
    

    This will not tell you what the date for each file is, though.

    If you want to get the modified date as well, you can use ftp_rawlist and parse the output. Here’s a quick example I scraped together:

    $list = ftp_rawlist($ftp, '.');
    
    $results = array();
    foreach ($list as $line) {
        list($perms, $links, $user, $group, $size, $d1, $d2, $d3, $name) =
            preg_split('/\s+/', $line, 9);
        $stamp = strtotime(implode(' ', array($d1, $d2, $d3)));
        $results[] = array('name' => $name, 'timestamp' => $stamp);
    }
    
    usort($results, function($a, $b) { return $a['timestamp'] - $b['timestamp']; });
    

    At this point $results contains a list sorted in ascending last modified time; reverse the sort function to get the list in most recently modified first format.

    Note: ftp_rawlist does not provide exact modification timestamps, so this code might not always work accurately. You should also verify that the output from your FTP server agrees with this algorithm and include some sanity checks to make sure things stay that way in the future.

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

Sidebar

Related Questions

Background: We have a TFS server setup where me manage our source code and
Background: I have a ListView / GridView with several columns. In some situations, only
Background: I have a css and a js that is used only by the
Background I have a secured folder containing secret report files (in pdf format). Each
BACKGROUND: I have a set of Posts that can be voted on. I'd like
Background I have an installation of VisualSVN on a server. under this, I have
Background: I have several builds running on a Windows Server 2003 R2 machine via
Background I have a dimension table that has a single record for each day.
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background I have an existing extension designed to accompany a browser-based game (The extension

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.