PHP Function Reference

PHP ftp_site() Function



The PHP ftp_site() function sends the given SITE command to the FTP server. SITE commands are not standardized, and vary from server to server. They are useful for handling OS specific features such as file permissions and group membership.

Syntax

ftp_site(ftp, command)

Parameters

ftp Required. Specify the FTP connection to use.
command Required. Specify the SITE command. Note that this parameter isn't escaped so there may be some issues with filenames containing spaces and other characters.

Return Value

Returns true on success or false on failure.

Example:

The example below shows the usage of ftp_site() 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";

    //trying to send SITE command
    if (ftp_site($ftp, "chmod 777 /temp/file.txt")) {
      echo "Command executed successfully\n";
    } else {
      die('Command failed');
    }

  } 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
Command executed successfully
Connection closed successfully!

❮ PHP FTP Reference