How can I fetch a list of file names from a Remote Server via FTP?

Mike

I need to display the file names in a webpage using PHP FTP, and the list has to be sorted by the timestamp. How can I do this?

Kostya Shkryob

You will need:

  1. Start connection with FTP server (ftp_connect).
  2. Login to FTP (ftp_login).
  3. Get list of files (ftp_nlist).
  4. Get timestamp for each file (ftp_mdtm).
  5. Sort files by timpestamp and exclude folders.

Please see my code example:

<?php
define('FTP_URL', '');
define('FTP_USERNAME', '');
define('FTP_PASSWORD', '');
define('FTP_DIRECTORY', '');

//Connect ot FTP
$ftp = ftp_connect(FTP_URL);
//Login to FTP
ftp_login($ftp, FTP_USERNAME, FTP_PASSWORD);
//Get files
$filesAndFolders = ftp_nlist($ftp, FTP_DIRECTORY);
$dates = array();
$files = array();
foreach ($filesAndFolders as $key => $file) {
    //Get date
    $date = ftp_mdtm($ftp , $file);
    if ($date !== -1) {//Filter folders
        $files[] =  $file;
        $dates[] = $date;
    }
}
//Sort files by date
array_multisort($dates, SORT_DESC, $files);

//Render files list
echo '<ul>';
foreach ($files as $file) {
    echo '<li>' . $file . '</li>';
}
echo '</ul>';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I copy file from local server to remote with creating directories which absent via SSH?

From Dev

How can I jq a line from a file from a remote server?

From Dev

How can i upload a file into a ftp server?

From Dev

How can i upload a file into a ftp server?

From Dev

How i can get list with names of all queues in remote websphere application server?

From Dev

How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

From Dev

How can I fetch the last-modified value of a remote file?

From Dev

How to get list of file names from a private remote ip

From Dev

How can I obtain a remote drive's free space, for SQL Server to write a .BAK file, via a command run from within SQL Server?

From Dev

How can I set to fetch only some branches from remote

From Dev

How can I set to fetch only some branches from remote

From Dev

How to upload file to remote FTP Server in grails

From Dev

How can I delete a file from an ftp server using a java program that are N weeks old

From Dev

How can I expose multiple remote server ports via sshuttle?

From Dev

How can I download the oldest file of an FTP server?

From Dev

How can I download the oldest file of an FTP server?

From Dev

FTP: How to know if a file on remote ftp server is complete

From Dev

How do I list file names on a server in a directory?

From Dev

How can I retrieve a list of SQL Server LocalDb names?

From Dev

How can I rsync from a server that is accessible via ssh *via a remote login server* to my local computer that does not have an external I.P. address?

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

Get the latest file from a remote server from an FTP in Unix

From Dev

How can I Configure localhost and remote server with single config file

From Dev

how can i deploy the jar file in remote server

From Dev

How can I sort a list of file names by some substring of the name?

From Dev

How can I sort a list of file names by some substring of the name?

From Dev

How to receive list of directories or file from ftp server with int-ftp:outbound-gateway?

From Dev

PHP - What is the best practice for getting the ftp list from the remote server?

From Dev

How to read list of file names from DB and upload them to a ftp site

Related Related

  1. 1

    How can I copy file from local server to remote with creating directories which absent via SSH?

  2. 2

    How can I jq a line from a file from a remote server?

  3. 3

    How can i upload a file into a ftp server?

  4. 4

    How can i upload a file into a ftp server?

  5. 5

    How i can get list with names of all queues in remote websphere application server?

  6. 6

    How can I download a file from a FTP server, then automatically delete it from the server once the download completes?

  7. 7

    How can I fetch the last-modified value of a remote file?

  8. 8

    How to get list of file names from a private remote ip

  9. 9

    How can I obtain a remote drive's free space, for SQL Server to write a .BAK file, via a command run from within SQL Server?

  10. 10

    How can I set to fetch only some branches from remote

  11. 11

    How can I set to fetch only some branches from remote

  12. 12

    How to upload file to remote FTP Server in grails

  13. 13

    How can I delete a file from an ftp server using a java program that are N weeks old

  14. 14

    How can I expose multiple remote server ports via sshuttle?

  15. 15

    How can I download the oldest file of an FTP server?

  16. 16

    How can I download the oldest file of an FTP server?

  17. 17

    FTP: How to know if a file on remote ftp server is complete

  18. 18

    How do I list file names on a server in a directory?

  19. 19

    How can I retrieve a list of SQL Server LocalDb names?

  20. 20

    How can I rsync from a server that is accessible via ssh *via a remote login server* to my local computer that does not have an external I.P. address?

  21. 21

    Get the latest file from a remote server from an FTP in Unix

  22. 22

    Get the latest file from a remote server from an FTP in Unix

  23. 23

    How can I Configure localhost and remote server with single config file

  24. 24

    how can i deploy the jar file in remote server

  25. 25

    How can I sort a list of file names by some substring of the name?

  26. 26

    How can I sort a list of file names by some substring of the name?

  27. 27

    How to receive list of directories or file from ftp server with int-ftp:outbound-gateway?

  28. 28

    PHP - What is the best practice for getting the ftp list from the remote server?

  29. 29

    How to read list of file names from DB and upload them to a ftp site

HotTag

Archive