Skip to content

ftp_get()

What is ftp_get()?

The ftp_get() function is a PHP built-in function that retrieves a file from the FTP server and saves it to a local file. The function takes five parameters (three required, two optional):

  1. ftp_stream: The connection identifier returned by the ftp_connect() function.
  2. local_file: The local file name to which the retrieved data should be saved.
  3. remote_file: The remote file name to be retrieved from the FTP server.
  4. mode: The transfer mode. Use FTP_BINARY for binary files or FTP_ASCII for text files.
  5. resumepos: The starting position of the transfer (defaults to 0).

The function returns a boolean value. If the function is successful in retrieving the file, it returns true. Otherwise, it returns false.

Syntax of ftp_get()

The syntax of the ftp_get() function is as follows:

Syntax of ftp_get() in PHP

php
bool ftp_get ( resource $ftp_stream , string $local_file , string $remote_file [, int $mode = FTP_BINARY [, int $resumepos = 0 ]] )

Note: In PHP 8.1+, the resource type for FTP connections is deprecated in favor of Ftp\Connection objects. The legacy resource syntax is retained here for backward compatibility.

The ftp_get() function takes five parameters: ftp_stream, local_file, remote_file, mode, and resumepos. The ftp_stream parameter is the connection identifier returned by the ftp_connect() function. The local_file parameter is the local file name to which the retrieved data should be saved. The remote_file parameter is the remote file name to be retrieved from the FTP server. The mode parameter is an optional parameter that specifies the transfer mode, and the resumepos parameter is an optional parameter that specifies the starting position of the transfer. FTP_BINARY is recommended for most files (images, archives, executables), while FTP_ASCII is only needed for plain text files and performs automatic line-ending conversions.

Usage of ftp_get()

To use the ftp_get() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:

Usage of ftp_get() in PHP

php
<?php
// Set up an FTP connection
$conn = @ftp_connect('ftp.example.com');
if (!$conn) {
    die("Could not connect to FTP server.");
}

// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
    die("Login failed.");
}

// Retrieve a remote file and save it to a local directory
if (ftp_get($conn, '/local/directory/local_file.txt', 'remote_file.txt', FTP_BINARY)) {
    echo "File retrieved successfully.\n";
} else {
    echo "Failed to retrieve the file.\n";
}

// Close the FTP connection
ftp_close($conn);

In this example, we establish a connection to the FTP server using the ftp_connect() function and verify it succeeded. Then we log in using our FTP credentials using the ftp_login() function and check for login errors. Finally, we retrieve a remote file and save it to a local directory using the ftp_get() function with the FTP_BINARY mode, and close the FTP connection.

Error handling in ftp_get()

It's important to handle errors properly when using the ftp_get() function. If the function returns false, it means that the file couldn't be retrieved for some reason. Here's an example of how to handle errors:

Error handling in ftp_get()

php
<?php
if (ftp_get($conn, '/local/directory/local_file.txt', 'remote_file.txt', FTP_ASCII) === false) {
    echo "Failed to retrieve the file.\n";
} else {
    echo "File retrieved successfully.\n";
}

In this example, we check the return value of the ftp_get() function. If it's false, we display an error message; otherwise, we display a success message.

Conclusion

In conclusion, the ftp_get() function is a useful PHP built-in function that allows you to retrieve a file from an FTP server and save it to a local file. By following the guidelines and best practices outlined in this article, you can use the ftp_get() function in your PHP projects with confidence.

Practice

What does the FTP_GET function in PHP do?

Dual-run preview — compare with live Symfony routes.