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);
Call
What 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
Parameter
Description
ftp
Required. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
size
Required. The number of bytes to allocate on the server.
response
Optional. A variable that receives the textual reply from the server, passed by reference.
Return Value
Return Value
Description
bool
true 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.
filesize() returns the size of the local file in bytes.
ftp_alloc() tells the server how much space the upload will need.
ftp_put() uploads the file in FTP_BINARY mode so it is not changed.
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().