W3docs

ftp_mdtm()

The ftp_mdtm() function is a PHP built-in function that retrieves the last modified time of the specified file on the FTP server. The function takes two

What is ftp_mdtm()?

The ftp_mdtm() function is a PHP built-in function that retrieves the last modified time of a specified file on an FTP server. It takes two parameters:

  1. ftp_stream: The connection identifier returned by ftp_connect().
  2. remote_file: The path to the file on the server.

The function returns the last modified time as a Unix timestamp. If the file doesn't exist or the connection is invalid, it returns -1.

Syntax of ftp_mdtm()

The syntax of the ftp_mdtm() function is as follows:

Syntax of ftp_mdtm()

int ftp_mdtm ( resource $ftp_stream , string $remote_file )

Note: The resource type is deprecated in PHP 8. In PHP 8.1+, the first parameter expects an FTP\Connection object instead.

This function requires an active FTP connection. It does not automatically handle passive mode; if your server requires it, call ftp_pasv($ftp_stream, true) before invoking this function.

Usage of ftp_mdtm()

To use the ftp_mdtm() function, first establish a connection to the FTP server using ftp_connect(). Here's an example:

Usage of ftp_mdtm()

<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');

// Login with your FTP credentials
ftp_login($conn, 'username', 'password');

// Get the last modified time of the remote file
$last_modified = ftp_mdtm($conn, '/path/to/remote/file');

// Close the FTP connection
ftp_close($conn);

In this example, we establish a connection, log in, retrieve the file's modification time, and close the connection.

Error handling in ftp_mdtm()

It's important to handle errors properly when using ftp_mdtm(). Since the function returns -1 on failure, you should check for that value. Here's an example:

Error handling in ftp_mdtm()

<?php

$last_modified = ftp_mdtm($conn, '/path/to/remote/file');

if ($last_modified === -1) {
    echo "Failed to retrieve the last modified time.\n";
} else {
    echo "The last modified time is $last_modified.\n";
}

This example checks the return value. If it equals -1, an error message is displayed; otherwise, the timestamp is shown.

Conclusion

The ftp_mdtm() function is a useful PHP built-in function that allows you to retrieve the last modified time of a specified file on an FTP server. By following the guidelines and best practices outlined in this article, you can use ftp_mdtm() in your PHP projects with confidence. We hope this article has been helpful to you.

Practice

Practice

What is the purpose of the 'ftp_mdtm' function in PHP?