Current Location: Home> Latest Articles> How to Use PHP's ftp_size() Function to Get FTP File Size

How to Use PHP's ftp_size() Function to Get FTP File Size

gitbox 2025-06-16

1. What is the ftp_size() Function?

The ftp_size()

Where $ftp_stream is the FTP connection resource established using the ftp_connect() function, and $remote_file is the name of the file whose size you want to retrieve.

2. Return Value of ftp_size() Function

When the function is called successfully, the ftp_size() function returns the size of the specified file. The size is returned as an integer in bytes. If the file does not exist or cannot be accessed, the function will return false.

Here is an example of the return value of ftp_size():


$ftp_conn = ftp_connect('ftp.example.com');
$ftp_login = ftp_login($ftp_conn, 'username', 'password');
$file_size = ftp_size($ftp_conn, 'example_file.txt');
if ($file_size === false) {
    echo 'Failed to get file size!';
} else {
    echo 'File size is: ' . $file_size . ' bytes';
}
        

In this example, first, the ftp_connect() function establishes a connection to the FTP server, then ftp_login() logs into the server. The ftp_size() function is called to retrieve the size of the specified file, and the result is stored in the $file_size variable.

Finally, it checks whether the $file_size variable is false. If it is, it indicates that the file size retrieval failed; otherwise, the file size is output to the screen.

3. Important Notes

3.1 FTP Extension Must Be Installed

Before using the ftp_size() function, make sure the FTP extension is installed in your PHP configuration. If it's not installed, the function will not work properly.

3.2 Case Sensitivity of File Names

When calling the ftp_size() function, make sure that the file name matches the exact case of the file on the server. If the case does not match, the function may fail to return the file size.

3.3 FTP Data Transfer Mode

The ftp_size() function uses binary (BINARY) transfer mode by default. If you need to get the size of a text file, you should set the transfer mode to ASCII before transferring.

You can use the ftp_pasv() function to set the FTP passive transfer mode. If your FTP server does not support passive mode, the transfer mode should be set to active mode.

4. Example

Here is a complete example of how to get the file size on an FTP server:


$ftp_conn = ftp_connect('ftp.example.com');
$ftp_login = ftp_login($ftp_conn, 'username', 'password');
ftp_pasv($ftp_conn, true);  // Enable passive mode
$get_size = ftp_size($ftp_conn, 'example_file.txt');
if ($get_size !== false) {
    echo "File size is: " . $get_size . " bytes";
} else {
    echo "Unable to get file size!";
}
ftp_close($ftp_conn);
        

In this example, the ftp_connect() function is used to connect to the FTP server, then ftp_login() logs in. After that, the ftp_size() function is used to retrieve the file size, and the result is output to the screen.

5. Conclusion

The ftp_size() function in PHP is a useful tool for getting the size of a specified file on an FTP server. It needs to be used in conjunction with other FTP-related functions, such as ftp_connect() and ftp_login(), to ensure proper connection and file retrieval. Whether you're checking file size or performing other FTP operations, ftp_size() provides an easy solution.