ftp_chmod()
The ftp_chmod() function is a PHP built-in function that allows you to change the permissions of a file or directory on a remote FTP server. The function takes
The PHP ftp_chmod() Function
ftp_chmod() is a built-in PHP function that changes the permission mode of a file or directory on a remote FTP server — the FTP equivalent of running chmod over a shell. It is part of PHP's FTP extension and is useful when a deployment or upload script must make a file readable, writable, or executable on the server after transferring it.
This page covers the syntax, what the mode value really means, a complete working flow, return-value handling, and the gotchas that trip people up. If you are new to the FTP extension, start with ftp_connect() and ftp_login().
Syntax
ftp_chmod(FTP\Connection $ftp, int $permissions, string $filename): int|falseThe parameters are:
$ftp— the connection handle. Up to PHP 7.4 this was aresourcereturned by ftp_connect(); since PHP 8.1 it is anFTP\Connectionobject, but you use it the same way.$permissions— the new permission mode as an octal integer (for example0644,0755).$filename— the path to the file or directory whose permissions you want to change.
Return value: on success the function returns the new file permissions as an integer; on failure it returns false. Always compare with === so a valid mode such as 0 (which is falsy) is not mistaken for failure.
Why permissions are written in octal
A common mistake is passing 644 instead of 0644. The leading 0 makes PHP read the number as octal, which is how Unix file permissions are expressed.
<?php
// 0644 (octal) is NOT the same as 644 (decimal)
var_dump(0644); // int(420) -> the value you actually want
var_dump(644); // int(644) -> wrong, this is 1204 in octal
// Each digit is owner / group / others:
// 6 = read + write (4 + 2)
// 4 = read only
// So 0644 means: owner can read & write, group and others can read.Use 0644 for regular files that should be readable by everyone but writable only by the owner, and 0755 for directories or executable scripts.
Basic usage
To change permissions you first connect with ftp_connect(), authenticate with ftp_login(), call ftp_chmod(), then release the connection with ftp_close().
<?php
// 1. Open a connection to the FTP server
$ftp = ftp_connect('ftp.example.com');
// 2. Log in with your credentials
ftp_login($ftp, 'username', 'password');
// 3. Make the file readable by all, writable by the owner
ftp_chmod($ftp, 0644, '/path/to/file.txt');
// 4. Close the connection
ftp_close($ftp);Checking the result
Because the FTP server may reject the request (wrong path, insufficient privileges, or a server that does not support SITE CHMOD), always check the return value:
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
$result = ftp_chmod($ftp, 0644, '/path/to/file.txt');
if ($result === false) {
echo "Failed to change permissions.\n";
} else {
// $result is the new mode; printf with %o shows it back in octal
printf("Permissions changed to %o successfully.\n", $result);
}
ftp_close($ftp);For 0644, this prints Permissions changed to 644 successfully. — the %o format specifier converts the returned integer back into the familiar octal notation.
Common gotchas
ftp_chmod()is not recursive. It affects a single path. To chmod a whole tree you must list the directory (see ftp_nlist()) and callftp_chmod()on each entry.- Not every server supports it.
ftp_chmod()relies on the FTPSITE CHMODcommand, which some servers (notably many Windows IIS FTP servers) do not implement. There it will simply returnfalse. - Pass octal, not decimal. As shown above,
644and0644are different numbers. - Order matters. You must be logged in before calling
ftp_chmod(); calling it on an unauthenticated connection fails.
Related functions
- ftp_connect() — open the FTP connection.
- ftp_login() — authenticate before any operation.
- ftp_put() / ftp_get() — upload and download files.
- ftp_close() — close the connection.
- chmod() — change permissions on the local filesystem.
Summary
ftp_chmod() changes the permission mode of a remote file or directory over FTP. Pass the mode as an octal integer (0644, 0755), check the return value with === against false, and remember that the operation is single-path only and depends on server support for SITE CHMOD.