PHP ftp_size() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_size() returns the size of a remote file in bytes.

It is handy for checking whether a file exists, showing download sizes, or comparing a remote file with a local one before you transfer it.

Syntax

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

PHP
$bytes = ftp_size($ftp, 'report.pdf');
CallWhat it does
ftp_size($ftp, 'report.pdf')Returns the size of report.pdf in bytes, such as 20480.
ftp_size($ftp, 'missing.txt')Returns -1 because the file cannot be found.
ftp_size($ftp, 'docs/readme.txt')Gets the size of a file inside a sub folder.

ftp_size() returns -1 on error, not false. It works on files only, and some servers return -1 for folders. Not every FTP server supports the SIZE command, though most do.

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

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in.
filenameRequired. The remote file whose size you want.

Return Value

Return ValueDescription
intReturns the file size in bytes, or -1 on error.

PHP Examples

Example 1 — Get a File Size

Ask the server how big a file is and print it in bytes.

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

$bytes = ftp_size($ftp, 'report.pdf');

echo "report.pdf is $bytes bytes", PHP_EOL;

ftp_close($ftp);

How It Works

  1. After logging in, ftp_size() sends the SIZE command for report. pdf.
  2. The server replies with the number of bytes in the file.
  3. That number is saved in the $bytes variable.
  4. echo prints the message with the size inside it.

Example 2 — Check If a File Exists

Since ftp_size() returns -1 for a missing file, you can use it as a quick existence check.

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

$size = ftp_size($ftp, 'missing.txt');

if ($size === -1) {
    echo "File not found", PHP_EOL;
} else {
    echo "File exists ($size bytes)", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_size() asks the server for the size of missing. txt.
  2. The server cannot find it, so PHP returns -1.
  3. === -1 checks for the error value without confusing it with a size of 0.
  4. The script prints that the file was not found.

Example 3 — Show Sizes in a Friendly Format

Loop over several files and convert the byte count to kilobytes.

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

$files = ['index.html', 'style.css', 'logo.png'];

foreach ($files as $file) {
    $size = ftp_size($ftp, $file);

    if ($size === -1) {
        echo "$file: not found", PHP_EOL;
        continue;
    }

    echo $file, ': ', round($size / 1024, 1), ' KB', PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The loop checks each file name from the $files array.
  2. A result of -1 prints not found and continue skips to the next file.
  3. Otherwise the size is divided by 1024 to turn bytes into kilobytes.
  4. round() keeps one decimal place so the output is easy to read.

Frequently Asked Questions

It returns the size of a remote file in bytes by sending the SIZE command to the FTP server.
It returns -1. It does not return false, so compare the result with === -1.
Usually not. Most servers return -1 for folders because SIZE is meant for files.
Yes, it is a quick check. A result of -1 means missing or unreadable, and any other number means the file exists.
The server may not support the SIZE command, or the file may not be readable by your user. Try ftp_rawlist() as a backup.

Next: ftp_ssl_connect()

ftp_ssl_connect() opens a secure FTP connection over SSL/TLS. It is the secure version of .

ftp_ssl_connect() 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