PHP FTP函数

PHP ftp_mdtm() 函数返回给定文件的最后修改时间。

注意:此函数不适用于目录。

语法

ftp_mdtm(ftp, filename) 

    参数

    ftp必填。 指定要使用的 FTP 连接。
    filename必填。 指定从中提取上次修改时间的文件。

    返回值

    成功时返回上次修改时间为本地 Unix 时间戳,错误时为 -1。

    示例:

    下面的示例显示了 ftp_mdtm() 函数的用法。

    <?php
    //要使用的FTP服务器
    $ftp_server = "ftp.example.com";
     
    $file = 'test.txt';
    
    //FTP 连接的用户名
    $ftp_user = "user";
      
    //用户密码
    $ftp_pass = "password";
      
    //建立连接或者连接失败
    $ftp = ftp_connect($ftp_server)
        or die("Could not connect to $ftp_server");
       
    if($ftp) {
      echo "Successfully connected to $ftp_server!\n";
     
      //尝试登录
      if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
        echo "Connected as $ftp_user@$ftp_server\n";
    
        //获取最后修改时间
        $lastmodified  = ftp_mdtm($ftp, $file);
    
        if ($lastmodified != -1) {
          echo "$file was last modified on : "
               .date("d-M-Y H:i:s", $lastmodified);
        } else {
          echo "Could not get the last modified time";
        }
        
      } else {
        echo "Couldn't connect as $ftp_user\n";
      }
     
      //关闭连接
      if(ftp_close($ftp)) {
        echo "Connection closed successfully!\n"; 
      } 
    }
    ?> 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    上述代码的输出将是:

    Successfully connected to ftp.example.com!
    Connected as user@ftp.example.com
    test.txt was last modified on : 14-Oct-2021 10:48:23
    Connection closed successfully! 
    • 1
    • 2
    • 3