ftp_chmod() function in PHP

The ftp_chmod() function sets permissions on a remote file via FTP. This function allows you to modify file permissions on an FTP server using octal notation, similar to the Unix chmod command.

Syntax

ftp_chmod(connection, mode, filename)

Parameters

  • connection − Required. The FTP connection resource

  • mode − Required. The new permissions in octal format

    It consists of four digits −

    • The first digit is always zero

    • The second digit specifies permissions for the owner

    • The third digit specifies permissions for the owner's group

    • The fourth digit specifies permissions for everyone else

    Permission values (add numbers for multiple permissions) −

    • 4 = read permissions

    • 2 = write permissions

    • 1 = execute permissions

  • filename − Required. The remote file to modify

Return Value

Returns the new file permissions on success, or FALSE on failure.

Common Permission Modes

Mode Owner Group Others Description
0644 rw- r-- r-- Read/write for owner, read-only for others
0755 rwx r-x r-x Full access for owner, read/execute for others
0777 rwx rwx rwx Full access for everyone

Example

The following example demonstrates how to change file permissions on an FTP server −

<?php
    $ftp_server = "192.168.0.4";
    $ftp_user = "username";
    $ftp_pass = "password";
    $remote_file = "demo.txt";
    
    // Establish FTP connection
    $connection = ftp_connect($ftp_server);
    $login = ftp_login($connection, $ftp_user, $ftp_pass);
    
    if ($connection && $login) {
        // Change file permissions to 0755 (rwxr-xr-x)
        if (ftp_chmod($connection, 0755, $remote_file) !== false) {
            echo "Permissions changed successfully to 0755<br>";
        } else {
            echo "Failed to change permissions<br>";
        }
        
        // Change to read-only for all (0444)
        if (ftp_chmod($connection, 0444, $remote_file) !== false) {
            echo "File set to read-only (0444)<br>";
        }
    } else {
        echo "FTP connection failed<br>";
    }
    
    ftp_close($connection);
?>
Permissions changed successfully to 0755
File set to read-only (0444)

Conclusion

The ftp_chmod() function provides a simple way to modify file permissions on FTP servers. Always verify the connection before attempting to change permissions, and use appropriate octal values based on your security requirements.

Updated on: 2026-03-15T07:38:59+05:30

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements