【问题标题】:How can I download the most recent file on FTP with PHP?如何使用 PHP 在 FTP 上下载最新的文件?
【发布时间】:2013-04-17 08:31:19
【问题描述】:

在 FTP 服务器上有一些文件。此服务器上的任何时间都在上传新文件。我想下载最后一个文件。如何从该服务器获取最后上传的文件?所有文件都有不同的名称。

我使用以下脚本下载一个文件。

$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"user","pass");
ftp_get($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);

提前致谢!!!

【问题讨论】:

  • 试过什么?逐步解释您认为的解决方案是什么(例如 1. 获取文件列表,2. 查找最新文件)并显示您遇到的问题。
  • 我总是使用proceed 文件夹,在那里我移动了我用 ftp 下载的文件。这使您无法检查时间戳

标签: php ftp


【解决方案1】:

没有办法确定哪个文件是最新的,因为没有“上传时间”属性这样的东西。您没有过多提及 FTP 服务器,但如果您对上传有一定程度的管理,您可以确保在上传时设置上次修改时间。这是否最终起作用取决于您的 FTP 服务器和可能的客户端。

假设您的修改时间等于上传时间,您可以执行以下操作:

// connect
$conn = ftp_connect('ftp.addr.com');
ftp_login($conn, 'user', 'pass');

// get list of files on given path
$files = ftp_nlist($conn, '');

$mostRecent = array(
    'time' => 0,
    'file' => null
);

foreach ($files as $file) {
    // get the last modified time for the file
    $time = ftp_mdtm($conn, $file);

    if ($time > $mostRecent['time']) {
        // this file is the most recent so far
        $mostRecent['time'] = $time;
        $mostRecent['file'] = $file;
    }
}

ftp_get($conn, "target.txt", $mostRecent['file'], FTP_ASCII);
ftp_close($conn);

【讨论】:

    【解决方案2】:
    ftp_rawlist($conn);
    

    提取最新的文件名并获取它。

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多