PHP Function Reference

PHP ftp_alloc() Function



The PHP ftp_alloc() function is used to allocate space for a file to be uploaded to the FTP server.

Syntax

ftp_alloc(ftp, size, response)

Parameters

ftp Required. Specify the FTP connection to use.
size Required. Specify the number of bytes to allocate.
response Optional. Specify a variable. A textual representation of the servers response is stored in this variable if provided.

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of ftp_alloc() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";
   
//set up a connection or die 
$ftp = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";

    //server file path where the file need to be uploaded
    $server_file = "server_demo.txt";

    //local file path which need to be uploaded
    $local_file = "local_demo.txt";

    if(ftp_alloc($ftp, filesize($local_file), $result)) {
      echo "Space successfully allocated on server.  Sending $local_file\n";
      ftp_put($ftp, $server_file, $local_file, FTP_BINARY);
    } else {
      echo "Unable to allocate space on server.  Server said: $result\n";
    }
    
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }
 
  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Space successfully allocated on server.  Sending local_demo.txt
Connection closed successfully!

❮ PHP FTP Reference