In PHP, ftp_put() uploads a file from your own computer or server to a remote FTP server. It is the function you use when you want to send a local file somewhere else.
You give it an open FTP connection, the name the file should have on the server, and the path of the local file. It waits until the whole upload is done, then returns true or false.
Syntax
Call ftp_put() with the values you need. Start with this line.
Uploads a binary file from the start, with the remote offset set to 0.
ftp_put() blocks until the upload finishes. For very large files, or if you want to do other work while uploading, look at ftp_nb_put(). Use FTP_BINARY for images, zips and PDFs, and FTP_ASCII only for plain text.
Library declarations
PHP
ftp_put(FTP\Connection $ftp, string $remote_filename, string $local_filename, int $mode = FTP_BINARY, int $offset = 0): bool
Parameters
Parameter
Description
ftp
Required. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in with ftp_login().
remote_filename
Required. The path and name the file will have on the FTP server.
local_filename
Required. The path to the local file you want to upload.
mode
Optional. The transfer mode, either FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offset
Optional. The position in the remote file where the upload should start. The default is 0.
Return Value
Return Value
Description
bool
Returns true on success or false on failure, for example if the local file does not exist or the server refuses the upload.
PHP Examples
Example 1 — Upload a Single File
Connect, log in, and upload a local text file to the server.
ftp_pasv(true) turns on passive mode, which works better behind most firewalls.
The loop goes through each file name in the $files array.
Text files use FTP_ASCII and everything else, such as images, uses FTP_BINARY.
Each call to ftp_put() prints ok or failed so you can see which upload had a problem.
Frequently Asked Questions
It uploads a local file to a remote FTP server. You pass the open connection, the remote file name, the local file path, and optionally the transfer mode.
Use FTP_BINARY for images, PDFs, zip files and anything that is not plain text. Use FTP_ASCII only for text files. When in doubt, binary is the safer choice.
Common reasons are a wrong local path, a remote folder that does not exist, missing write permission, or a firewall blocking the data connection. Try ftp_pasv($ftp, true) first.
Yes. If a file with the same remote name already exists, it is replaced. Use ftp_size() first if you want to check whether the file is already there.
ftp_put() reads from a file path on disk, while ftp_fput() reads from an open file handle. ftp_put() is simpler for most beginner tasks.