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.
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():
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.
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.
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.
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.
Here is a complete example of how to get the file size on an FTP server:
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.
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.