Current Location: Home> Latest Articles> PHP ftp_size() Function Explained: Get FTP File Size

PHP ftp_size() Function Explained: Get FTP File Size

M66 2025-07-11

PHP ftp_size() Function Explained: Get FTP File Size

The ftp_size() function is used to get the size of a specific file on an FTP server. This article will provide a detailed explanation of the function's syntax, parameters, and how to use it in your code to get a file's size.

Syntax

ftp_size(conn, myfile)

Parameters

  • conn — FTP connection resource
  • myfile — The file path on the server

Return Value

The ftp_size() function returns the size of the specified file in bytes if successful. If it fails, it returns -1.

Example

Here is a simple example that demonstrates how to use the ftp_size() function to get the file size on an FTP server:

<?php
$ftp_server = "192.168.0.4";
$ftp_user = "username";
$ftp_pass = "gfhgfj236k";
$conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($conn, $ftp_user, $ftp_pass);
$myfile = "new.txt";
// Get file size
$size = ftp_size($conn, $myfile);
if ($size != -1) {
    echo "Size is $size bytes";
} else {
    echo "Cannot get the file size!";
}
// Close connection
ftp_close($conn);
?>

Conclusion

By using PHP's ftp_size() function, developers can easily retrieve the size of a specified file on an FTP server. This is useful when managing FTP files or automating file handling tasks. For more FTP-related tutorials in PHP, refer to other related articles.