PHP Function Reference

PHP ftp_mdtm() Function



The PHP ftp_mdtm() function returns the last modified time of the given file.

Note: This function does not work with directories.

Syntax

ftp_mdtm(ftp, filename)

Parameters

ftp Required. Specify the FTP connection to use.
filename Required. Specify the file from which to extract the last modification time.

Return Value

Returns the last modified time as a local Unix timestamp on success, or -1 on error.

Example:

The example below shows the usage of ftp_mdtm() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";
 
$file = 'test.txt';

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
  
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //getting the last modified time
    $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";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

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!

❮ PHP FTP Reference