PHP ftp_fput() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_fput() uploads data from an open local file handle to a file on the FTP server. It is the stream version of ftp_put().

You open a file or memory stream yourself and pass the handle, so you can upload text built in memory without creating a temporary file on disk. The upload starts from the current position of the handle.

Syntax

Call ftp_fput() with the values you need. Start with this line.

PHP
$ok = ftp_fput($ftp, "remote.txt", $handle, FTP_ASCII);
CallWhat it does
ftp_fput($ftp, "remote.txt", $handle, FTP_ASCII)Uploads the text from the handle to remote.txt.
ftp_fput($ftp, "photo.jpg", $handle, FTP_BINARY)Uploads an image without changing any bytes.
ftp_fput($ftp, "big.zip", $handle, FTP_BINARY, 1024)Starts writing to the remote file at byte 1024 (resume).

The handle must be opened for reading and be at the position where you want to start, so use rewind() if you wrote data to it first. From PHP 8.1 the first argument is an FTP\Connection object.

Library declarations
PHP
ftp_fput(FTP\Connection $ftp, string $remote_filename, resource $stream, int $mode = FTP_BINARY, int $offset = 0): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
remote_filenameRequired. The path of the file on the server where the data will be stored.
streamRequired. An open file pointer to read the data from.
modeOptional. The transfer mode, FTP_ASCII or FTP_BINARY. The default is FTP_BINARY.
offsetOptional. The position in the remote file to start uploading to. The default is 0.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Upload From a File Handle

Open a local file for reading and send it to the server. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$handle = fopen("notes.txt", "r");

if (ftp_fput($ftp, "uploads/notes.txt", $handle, FTP_ASCII)) {
    echo "Uploaded notes.txt", PHP_EOL;
}

fclose($handle);
ftp_close($ftp);

How It Works

  1. fopen() with mode r opens the local file for reading.
  2. ftp_fput() reads from the handle and sends the data to uploads/notes. txt.
  3. A true result means the upload finished.
  4. fclose() closes the file and ftp_close() ends the session.

Example 2 — Upload Text From Memory

Build text in a memory stream, rewind it, and upload it without creating a local file. Forgetting rewind() uploads nothing.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$handle = fopen("php://temp", "r+");
fwrite($handle, "Hello from PHP!");
rewind($handle);

$ok = ftp_fput($ftp, "hello.txt", $handle, FTP_ASCII);

var_dump($ok);
echo "Remote size: ", ftp_size($ftp, "hello.txt"), " bytes", PHP_EOL;

fclose($handle);
ftp_close($ftp);

How It Works

  1. php://temp creates a temporary in-memory stream.
  2. fwrite() puts the text inside it, and rewind() moves the pointer back to the start.
  3. ftp_fput() uploads the 15 characters to hello. txt.
  4. ftp_size() confirms the remote file is 15 bytes long.

Example 3 — Upload a Generated CSV

Create a small CSV in memory with fputcsv() and send it to the server.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$rows = [
    ["name", "score"],
    ["Asha", 90],
    ["Ravi", 85],
];

$handle = fopen("php://temp", "r+");

foreach ($rows as $row) {
    fputcsv($handle, $row);
}

rewind($handle);

echo ftp_fput($ftp, "scores.csv", $handle, FTP_ASCII)
    ? "scores.csv uploaded"
    : "Upload failed";

echo PHP_EOL;

fclose($handle);
ftp_close($ftp);

How It Works

  1. We build an array of rows with a header and two records.
  2. fputcsv() writes each row into the in-memory stream as a CSV line.
  3. rewind() resets the pointer so ftp_fput() reads from the start.
  4. ftp_fput() uploads the CSV in FTP_ASCII mode since it is plain text.

Frequently Asked Questions

It uploads data from an open local file handle to a file on the FTP server. It returns true on success and false on failure.
ftp_put() takes a local file path. ftp_fput() takes an open file handle, so you can upload data from memory or any stream.
The handle pointer was probably at the end of the data. Call rewind($handle) before ftp_fput() if you wrote data to the stream first.
Use FTP_ASCII for text files and FTP_BINARY for images, archives, PDFs and other binary data.
Yes, call fclose() when you are done. ftp_fput() does not close the handle for you.

Next: ftp_get()

ftp_get() downloads a file from the FTP server and saves it to a local file. It is the eas.

ftp_get() 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