PHP ftp_put() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

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.

PHP
$ok = ftp_put($ftp, 'report.pdf', __DIR__ . '/report.pdf', FTP_BINARY);
CallWhat it does
ftp_put($ftp, 'a.txt', 'a.txt')Uploads a.txt using the default binary mode and returns true on success.
ftp_put($ftp, 'notes.txt', 'notes.txt', FTP_ASCII)Uploads a text file in ASCII mode so line endings are converted when needed.
ftp_put($ftp, 'big.zip', 'big.zip', FTP_BINARY, 0)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

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in with ftp_login().
remote_filenameRequired. The path and name the file will have on the FTP server.
local_filenameRequired. The path to the local file you want to upload.
modeOptional. The transfer mode, either FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offsetOptional. The position in the remote file where the upload should start. The default is 0.

Return Value

Return ValueDescription
boolReturns 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.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$local  = __DIR__ . '/hello.txt';
$remote = 'hello.txt';

if (ftp_put($ftp, $remote, $local, FTP_ASCII)) {
    echo "Uploaded $remote", PHP_EOL;
} else {
    echo "Upload failed", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_connect() opens the connection and ftp_login() signs in with a username and password.
  2. $local holds the path of the file on your machine and $remote is the name it gets on the server.
  3. ftp_put() sends the file in ASCII mode and returns true when the server accepts it.
  4. ftp_close() ends the session once the upload is finished.

Example 2 — Handle a Missing Local File

ftp_put() returns false when the local file cannot be read, so check it before you upload.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$local = __DIR__ . '/missing.txt';

if (!is_file($local)) {
    echo "Local file not found", PHP_EOL;
} elseif (ftp_put($ftp, 'missing.txt', $local, FTP_BINARY)) {
    echo "Uploaded", PHP_EOL;
} else {
    echo "Upload failed", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. is_file() checks that the local file really exists before any upload is tried.
  2. The file is missing, so the first branch prints a clear message.
  3. ftp_put() is never called, which avoids a confusing warning from PHP.
  4. The connection is closed neatly at the end.

Example 3 — Upload Many Files in a Loop

Loop over a list of local files and upload each one, picking the mode from the file extension.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');
ftp_pasv($ftp, true);

$files = ['index.html', 'style.css', 'logo.png'];

foreach ($files as $file) {
    $ext  = pathinfo($file, PATHINFO_EXTENSION);
    $mode = in_array($ext, ['html', 'css', 'txt']) ? FTP_ASCII : FTP_BINARY;

    $ok = ftp_put($ftp, $file, __DIR__ . '/' . $file, $mode);
    echo $file, ': ', $ok ? 'ok' : 'failed', PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_pasv(true) turns on passive mode, which works better behind most firewalls.
  2. The loop goes through each file name in the $files array.
  3. Text files use FTP_ASCII and everything else, such as images, uses FTP_BINARY.
  4. 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.

Next: ftp_pwd()

ftp_pwd() returns the current working directory on the FTP server. PWD stands for print wo.

ftp_pwd() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful