Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
706
Q:

PHP ftp_size() function (with example)

I am trying to use the `ftp_size()` function in PHP to retrieve the size of a file on an FTP server. However, I am facing some difficulties and need assistance.

Here is an example of my code:

```php
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$file_path = "/path/to/file.txt";

// Connect to FTP server
$conn = ftp_connect($ftp_server);
ftp_login($conn, $ftp_username, $ftp_password);

// Get the size of the file
$file_size = ftp_size($conn, $file_path);

// Close FTP connection
ftp_close($conn);

echo "The size of the file is: " . $file_size . " bytes";
?>
```

When I execute this code, I'm not getting the expected result. Instead, I'm getting a warning message saying "Warning: ftp_size(): Size not supportable".

I have checked that the FTP server supports the `SIZE` command and the file path is correct. Could someone please guide me on why I am getting this warning and how I can fix it? Am I missing any additional configuration or is my code incorrect?

Thanks in advance for any help provided!

All Replies

blynch

I faced a similar issue with the `ftp_size()` function when trying to retrieve the file size from an FTP server. Throughout my troubleshooting process, I found that the warning "Warning: ftp_size(): Size not supportable" can occur due to various reasons.

One potential reason for this warning is that the FTP server you're connecting to might not have the `SIZE` command enabled or supported. It could be that the FTP server has a different command to retrieve file size or doesn't provide this information at all.

To address this issue, I recommend trying an alternative approach using the `ftp_systype()` function. This function retrieves information about the connected FTP server, including its system type. By examining the system type, you may find a specific command or method to retrieve the file size.

Here's an updated version of your code incorporating the `ftp_systype()` function:

php
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$file_path = "/path/to/file.txt";

// Connect to FTP server
$conn = ftp_connect($ftp_server);
ftp_login($conn, $ftp_username, $ftp_password);

// Get the system type
$system_type = ftp_systype($conn);

// Check the system type to determine the appropriate command
if (strpos($system_type, "UNIX") !== false) {
$file_size = ftp_size($conn, $file_path);
} elseif (strpos($system_type, "Windows") !== false) {
$file_size = ftp_raw($conn, "SIZE $file_path")[1];
$file_size = explode(" ", $file_size)[1];
} else {
$file_size = "Unknown";
}

// Close FTP connection
ftp_close($conn);

echo "The size of the file is: " . $file_size . " bytes";
?>


By incorporating the `ftp_systype()` function, we can obtain information about the FTP server's system type. Based on this, we determine the appropriate command to retrieve the file size. In this case, if the system type is identified as UNIX, we can use the `ftp_size()` function. Otherwise, if it's identified as Windows, we send a raw FTP command with `ftp_raw()` and extract the file size from the response.

I hope this alternative approach helps you overcome the warning message and successfully retrieve the file size. Feel free to reach out if you have any further doubts or questions!

king.janelle

I faced a similar issue with the `ftp_size()` function in the past, and I managed to resolve it. The warning message "Warning: ftp_size(): Size not supportable" usually occurs when the FTP server does not support the `SIZE` command.

To troubleshoot this, you may want to try a different approach. Instead of relying solely on the `ftp_size()` function, you can use the `ftp_raw()` function to send a raw FTP command to the server and retrieve the file size.

Here's an example of how you can modify your code:

php
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$file_path = "/path/to/file.txt";

// Connect to FTP server
$conn = ftp_connect($ftp_server);
ftp_login($conn, $ftp_username, $ftp_password);

// Send raw FTP command to retrieve file size
$response = ftp_raw($conn, "SIZE $file_path");

// Extract the file size from the response
if (!empty($response)) {
$file_size = explode(" ", $response[0])[1];
} else {
$file_size = "Unknown";
}

// Close FTP connection
ftp_close($conn);

echo "The size of the file is: " . $file_size . " bytes";
?>


By using the `ftp_raw()` function, we send the `SIZE` command directly to the FTP server, even if the `ftp_size()` function is not supported. The response will contain the file size, which we can extract using string manipulation.

Give this approach a try, and hopefully, it will help you retrieve the file size without the warning message. Let me know if you have any further questions!

New to LearnPHP.org Community?

Join the community