PHP ftp_alloc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_alloc() asks the FTP server to reserve a number of bytes for a file you are about to upload. It sends the ALLO command behind the scenes.

Some servers need to know the size of an upload in advance so they can set aside disk space. Many modern servers do not need this and simply reply that no allocation is necessary, so ftp_alloc() is not often required.

Syntax

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

PHP
$ok = ftp_alloc($ftp, 1048576, $response);
CallWhat it does
ftp_alloc($ftp, 1048576)Asks the server to reserve 1 MB and returns true or false.
ftp_alloc($ftp, filesize($file), $response)Reserves space for a local file and stores the server reply in $response.
ftp_alloc($ftp, 0)Sends a size of zero, which most servers accept without doing anything.

From PHP 8.1 the first argument is an FTP\Connection object (before that it was a resource). The third argument is passed by reference and receives the text reply from the server.

Library declarations
PHP
ftp_alloc(FTP\Connection $ftp, int $size, string &$response = null): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
sizeRequired. The number of bytes to allocate on the server.
responseOptional. A variable that receives the textual reply from the server, passed by reference.

Return Value

Return ValueDescription
booltrue if the server accepted the allocation request, otherwise false.

PHP Examples

Example 1 — Reserve Space Before an Upload

Ask the server to reserve space for a file, then print the reply. The host and login are placeholders, and the output is typical for a working server.

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

$size = 2048;

if (ftp_alloc($ftp, $size, $response)) {
    echo "Space reserved: ", $response, PHP_EOL;
} else {
    echo "Allocation failed: ", $response, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_connect() and ftp_login() open and authenticate the connection.
  2. ftp_alloc() asks the server to reserve 2048 bytes.
  3. The server reply is saved in $response because it is passed by reference.
  4. ftp_close() ends the session when we are done.

Example 2 — Server Says No Allocation Is Needed

Many servers reply that no allocation is necessary. Read the response text instead of only checking true or false.

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

$result = ftp_alloc($ftp, 500000, $response);

var_dump($result);
echo trim($response), PHP_EOL;

ftp_close($ftp);

How It Works

  1. We ask for 500000 bytes of space.
  2. The server does not need allocation, so it replies with code 202.
  3. ftp_alloc() still returns true because the command was accepted.
  4. trim() removes the trailing line break from the response before printing.

Example 3 — Allocate Space Based on a Local File

Use filesize() so the reserved space matches the file you plan to upload.

PHP
<?php
$localFile = "report.pdf";
$remoteFile = "uploads/report.pdf";

$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$bytes = filesize($localFile);
ftp_alloc($ftp, $bytes);

if (ftp_put($ftp, $remoteFile, $localFile, FTP_BINARY)) {
    echo "Uploaded {$bytes} bytes", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. filesize() returns the size of the local file in bytes.
  2. ftp_alloc() tells the server how much space the upload will need.
  3. ftp_put() uploads the file in FTP_BINARY mode so it is not changed.
  4. The size is printed after a successful upload.

Frequently Asked Questions

It sends the ALLO command to the FTP server to reserve a number of bytes for an upcoming file. It returns true if the server accepts the request.
No. Most modern servers do not require it. It only helps with older servers that want to know the file size before the transfer starts.
Some servers do not support the ALLO command or treat it as unnecessary and reply with an error. The upload itself usually still works, so check the response text.
It is passed by reference and receives the text reply from the server, such as 202 No storage allocation necessary. It is useful for debugging.
From PHP 8.1 it is an FTP\Connection object. In PHP 8.0 and older it was a resource, but you still just pass the value returned by ftp_connect().

Next: ftp_append()

ftp_append() adds the contents of a local file to the end of a file that is already on the.

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