PHP Function Reference

PHP ftp_size() Function



The PHP ftp_size() function returns the size of the given file in bytes.

Syntax

ftp_size(ftp, filename)

Parameters

ftp Required. Specify the FTP connection to use.
filename Required. Specify the server file to check.

Return Value

Returns the file size on success, or -1 on error.

Example:

The example below shows the usage of ftp_size() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //file to check
    $file = "demo.txt";

    //get the size of the file
    $file_size = ftp_size($ftp, $file);
    if ($file_size != -1) {
      echo "$file has $file_size bytes\n";
    } else {
      echo "Error getting file size\n";
    }
    
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
demo.txt has 8168 bytes
Connection closed successfully!

❮ PHP FTP Reference