PHP ftp_mdtm() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_mdtm() returns the last modified time of a file on an FTP server. The result is a Unix timestamp, so you can format it with date().

It is handy when you want to know if a remote file changed, for example before you download it again. Remember that not every FTP server supports the MDTM command, and it does not work on directories.

Syntax

Call ftp_mdtm() with the values you need. Start with this line.

PHP
$time = ftp_mdtm($ftp, 'remote/file.txt');
CallWhat it does
ftp_mdtm($ftp, 'file.txt')Returns the Unix timestamp of file.txt in the current remote directory.
ftp_mdtm($ftp, '/public_html/index.php')Works with an absolute remote path too.
date('Y-m-d H:i:s', ftp_mdtm($ftp, 'file.txt'))Turns the timestamp into a readable date.

ftp_mdtm() returns -1 when it fails, for example when the file does not exist or the server does not support MDTM. It never returns false, so compare with -1.

Library declarations
PHP
ftp_mdtm(FTP\Connection $ftp, string $filename): int

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
filenameRequired. The path of the remote file whose modification time you want.

Return Value

Return ValueDescription
intThe last modified time as a Unix timestamp, or -1 on error.

PHP Examples

Example 1 — Show When a Remote File Changed

Read the modification time of a file and print it as a readable date.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$time = ftp_mdtm($ftp, 'report.txt');

echo "Last modified: ", date('Y-m-d H:i:s', $time), PHP_EOL;

ftp_close($ftp);

How It Works

  1. The script connects and logs in with placeholder credentials.
  2. ftp_mdtm() asks the server for the modified time of report. txt.
  3. The server answers with a Unix timestamp.
  4. date() converts that number into a year-month-day format.

Example 2 — Detect a Missing File

When the file does not exist, ftp_mdtm() returns -1. Check for it before using the value.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$time = ftp_mdtm($ftp, 'does-not-exist.txt');

if ($time === -1) {
    echo "File not found or MDTM not supported", PHP_EOL;
} else {
    echo "Modified: ", date('c', $time), PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The remote file name does not exist on the server.
  2. ftp_mdtm() returns -1 to signal an error.
  3. The strict comparison === -1 catches that case.
  4. A friendly message is printed instead of a wrong 1969 date.

Example 3 — Download Only If the Remote File Is Newer

Compare the remote timestamp with a local file and download only when the server copy is newer.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$remote = 'data.csv';
$local  = 'data.csv';

$remoteTime = ftp_mdtm($ftp, $remote);
$localTime  = file_exists($local) ? filemtime($local) : 0;

if ($remoteTime !== -1 && $remoteTime > $localTime) {
    ftp_get($ftp, $local, $remote, FTP_BINARY);
    echo "Downloaded newer file", PHP_EOL;
} else {
    echo "Local file is up to date", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_mdtm() gets the remote timestamp and filemtime() gets the local one.
  2. If the local file does not exist, its time is treated as 0.
  3. The remote file is downloaded only when it is newer and the call did not fail.
  4. This saves bandwidth because unchanged files are skipped.

Frequently Asked Questions

It returns the last modified time of a remote file as a Unix timestamp. On error it returns -1.
The file may not exist, you may not have permission, or the server may not support the MDTM command. Check the path and the server features.
No. The MDTM command only works on files. Use ftp_mlsd() if you need modification facts for directory entries.
Pass the timestamp to date(), for example date('Y-m-d H:i:s', $time). Do this only after you confirm the value is not -1.
The timestamp itself is absolute. Servers usually report it in UTC, and PHP formats it using your configured date.timezone when you call date().

Next: ftp_mkdir()

ftp_mkdir() creates a new directory on an FTP server. On success it returns the name of th.

ftp_mkdir() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful