Current Location: Home> Latest Articles> PHP Command Line FTP File Transfer Guide: Connect, Upload, Download, Close Connection

PHP Command Line FTP File Transfer Guide: Connect, Upload, Download, Close Connection

M66 2025-06-20

PHP Command Line FTP File Transfer Guide: Connect, Upload, Download, Close Connection

As the internet continues to evolve, file transfer has become one of the most frequently used features in everyday work. FTP (File Transfer Protocol), as a traditional file transfer protocol, is still widely used. This article will show you how to implement FTP file transfer using PHP via the command line, covering how to connect to the FTP server, upload and download files, and close the connection once the transfer is complete.

1. Connecting to the FTP Server

Before performing file transfers, you first need to establish a connection with the FTP server. PHP has a built-in FTP extension that allows you to do this with simple code.

<?php
// Connect to the FTP server
$ftp_server = 'ftp.example.com';
$user_name = 'username';
$user_pass = 'password';

$ftp_conn = ftp_connect($ftp_server) or die("Unable to connect to the server");
$login = ftp_login($ftp_conn, $user_name, $user_pass);

if (!$ftp_conn || !$login) {
    die("Unable to connect to the FTP server or login failed");
} else {
    echo "Successfully connected to the FTP server";
}
?>

In the above code, we use the `ftp_connect()` function to connect to the specified FTP server, and then use the `ftp_login()` function to authenticate the login. If the connection and login are successful, we can proceed with the file transfer.

2. Uploading Files

Uploading files is a common requirement in FTP file transfers. The following code example shows how to upload a file to the FTP server:

<?php
// Upload file
$file_path = 'path/to/local/file.txt';
$remote_file = 'path/to/remote/file.txt';

if (ftp_put($ftp_conn, $remote_file, $file_path, FTP_ASCII)) {
    echo "File uploaded successfully";
} else {
    echo "File upload failed";
}
?>

In this code, we use the `ftp_put()` function to upload a file. The `$remote_file` parameter specifies the file path and name where the file will be stored on the FTP server, while `$file_path` is the local file's path. The `FTP_ASCII` parameter indicates that the file should be uploaded in ASCII mode.

3. Downloading Files

In addition to uploading files, downloading files is also a common task in FTP file transfers. Below is an example code for downloading a file from the FTP server:

<?php
// Download file
$remote_file = 'path/to/remote/file.txt';
$file_path = 'path/to/local/file.txt';

if (ftp_get($ftp_conn, $file_path, $remote_file, FTP_ASCII)) {
    echo "File downloaded successfully";
} else {
    echo "File download failed";
}
?>

In this code, we use the `ftp_get()` function to download a file. The `$remote_file` parameter specifies the path and name of the file to be downloaded from the FTP server, while `$file_path` is the local path where the file will be saved. As with uploading, we use the `FTP_ASCII` mode for downloading.

4. Closing the Connection

After completing the file transfer, it is important to close the FTP connection to release resources. The following code shows how to close the connection:

<?php
// Close connection
ftp_close($ftp_conn);
?>

Using the `ftp_close()` function, we can safely close the FTP connection.

Summary:

Through the code examples provided in this article, you can see that performing FTP file transfers via PHP command line is straightforward. By using PHP’s FTP extension functions, you can easily connect to the FTP server, upload and download files, and close the connection. Depending on your needs, you can also extend these basic functionalities to add other operations.

We hope this article helps you better understand how to work with PHP and FTP, and that you can quickly perform file transfers via the command line.