PHP Function Reference

PHP ftp_chmod() Function



The PHP ftp_chmod() function sets the permissions on the specified remote file to permissions. The function returns the new file permissions on success or false on error.

Syntax

ftp_chmod(ftp, permissions, filename)

Parameters

ftp Required. Specify the FTP connection to use.
permissions Required. Specify the new permissions, given as an octal value (starting with 0). The parameter consists of four numbers:
  • The first number is always zero (octal value)
  • The second number specifies permissions for the OWNER
  • The third number specifies permissions for the OWNER's USER GROUP
  • The fourth number specifies permissions for EVERYBODY ELSE
Possible values (to set multiple permissions, add up the following numbers):
  • 1 = execute permissions
  • 2 = write permissions
  • 4 = read permissions
filename Required. Specify the file to set permissions on.

Return Value

Returns the new file permissions on success or false on error.

Example:

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

<?php
//FTP server to use
$ftp_server = "ftp.example.com";
  
//file for which permission need to set
$file = "/home/etc/test.txt";

//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 chmod $file to 644
    if (ftp_chmod($ftp, 0644, $file)) {
      echo "$file chmoded successfully to 644\n";
    } else {
      echo "Could not chmod $file.\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
/home/etc/test.txt chmoded successfully to 644
Connection closed successfully!

❮ PHP FTP Reference