Current Location: Home> Latest Articles> PHP Tutorial: Comprehensive Guide to ftp_put() for FTP File Uploads

PHP Tutorial: Comprehensive Guide to ftp_put() for FTP File Uploads

gitbox 2025-08-08

Introduction

FTP (File Transfer Protocol) is a standard protocol for transferring files over a network. In PHP development, FTP connection and file upload are common tasks. PHP includes the FTP extension with various functions, among which the ftp_put() function is essential for uploading local files to an FTP server.

The ftp_put() function uploads a local file to a remote FTP server and returns a boolean value indicating whether the upload was successful. The basic syntax is as follows:

<span class="fun">bool ftp_put(resource $ftp_stream, string $remote_file, string $local_file, int $mode [, int $startpos = 0])</span>

Function Parameters Explained

$ftp_stream is the FTP connection resource returned by ftp_connect() or ftp_ssl_connect().

$remote_file specifies the target filename on the FTP server.

$local_file is the full path and name of the local file to upload.

$mode determines the transfer mode, mainly two options: FTP_ASCII for text files, and FTP_BINARY for binary files such as images and videos.

$startpos specifies the start position of the upload, default is 0, meaning upload from the beginning of the file.

Example Usage

The following example demonstrates how to upload a file using ftp_put():

$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$local_file = '/path/to/file.jpg';
$remote_file = 'file.jpg';
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_username, $ftp_password);
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
    echo "File successfully uploaded to the FTP server\n";
} else {
    echo "File upload failed\n";
}
ftp_close($conn_id);

This code connects to the FTP server, uploads the local file /path/to/file.jpg to the server with the name file.jpg, using binary transfer mode. Success or failure messages will be output accordingly.

Error Handling

During the use of ftp_put(), various errors may occur such as missing files, insufficient permissions, server space limitations, or connection timeouts. To enhance upload stability and safety, it's useful to check the server directory before uploading, for example:

// Change to the server directory
ftp_chdir($conn_id, '/upload');
// Retrieve the list of files in the current directory
$file_list = ftp_nlist($conn_id, '.');
// If the target file exists, rename it as a backup
if (in_array('file.jpg', $file_list)) {
    ftp_rename($conn_id, 'file.jpg', 'file_backup.jpg');
}
// Upload the file
if (ftp_put($conn_id, 'file.jpg', $local_file, FTP_BINARY)) {
    echo "File successfully uploaded to the FTP server\n";
} else {
    echo "File upload failed\n";
}

This example changes to the /upload directory, checks if file.jpg exists, renames it to avoid overwriting, and then uploads the new file.

Important Usage Tips

When using ftp_put() to upload files, keep in mind the following:

  • Ensure the FTP server address, username, and password are correct.
  • The local file must exist and have read permissions.
  • Match the target filename with the server's naming conventions.
  • Avoid unintentionally overwriting important files on the server.
  • Make sure the file size fits within the server's storage limits.

Careful debugging and testing help avoid upload interruptions or failures, ensuring a smooth file transfer process.

Conclusion

The PHP ftp_put() function is a vital tool for FTP file uploads. This article covered its usage, parameters, example code, and error handling strategies, offering practical guidance to developers for efficient FTP file transfers.