Question : get list of files in a directory on a remote ftp server

I am using the php_ftp class to try to download a file from a remote server. The php_ftp class (see http://www.codewalkers.com/c/a/Miscellaneous-Code/PHPFTP/ ) handles permissions, but I don't know the name of the file that I need to download from the remote server.

Therefore, I need a program that will give me a list of the files in a directory on that remote server. I have the password and user name. The methods that I have tried, scandir and glob, don't seem to have a provision to handle providing the required user name and password.

I am specifically looking for the latest file in that directory, but if I can get any kind of a list, I can do the sorting.

If you can point me in the right direction, I'd appreciate it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
This is not tested, since I don't know the name of the $filename to be able to test it, so if you see an error, please let me know.

I need to be able to find the name of the latest file, so I have a value to put in $filename.


$dir="path/to/remote/directory/"
$mypath="path/to/local/directory/"

require("/home/mydir/public_html/class_files/class.FTP.php"); 
$f=new PHP_FTP('prftp01.remoteserver.com/outgoing', 'yyyyy', 'xxxxx',21); //where yyyyy is the user name and xxxxx is the password
$f->get($dir.$filename, $mypath.$filename); 
$f->kill();

Answer : get list of files in a directory on a remote ftp server

Sorry I always forget this doesn't parse code automatically.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
<?php
date_default_timezone_set('Europe/London');
$directory = './';
$ftp = ftp_connect('ftp.fo2.in');
ftp_login($ftp, 'fo2.in', 'wKYDXkfgK');
$files = ftp_nlist($ftp, $directory);
$newest_file = false;
$newest_time = 0;
foreach($files as &$file) {
  if($file != '.' && $file != '..') {
    $time = ftp_mdtm($ftp, $file);
    if ($newest_time < $time) {
      $newest_time = $time;
      $newest_file = $file;
    }
  }
}
ftp_get($ftp, "./{$newest_file}", $directory.$newest_file, FTP_BINARY);
?>
Random Solutions  
 
programming4us programming4us