PHP ftp_chmod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_chmod() sets the permissions of a file or folder on the FTP server. It sends the SITE CHMOD command.

Permissions decide who can read, write or run a file. They are written as an octal number such as 0644 for a normal file or 0755 for a folder or script. On success, ftp_chmod() returns the new permissions as an integer.

Syntax

Call ftp_chmod() with the values you need. Start with this line.

PHP
$mode = ftp_chmod($ftp, 0644, "index.php");
CallWhat it does
ftp_chmod($ftp, 0644, "index.php")Makes the file readable by everyone and writable by the owner only.
ftp_chmod($ftp, 0755, "uploads")Sets typical folder permissions.
ftp_chmod($ftp, 0777, "cache")Gives full access to everyone. Use it with care.

Write the permissions as an octal literal that starts with 0, like 0755. Writing 755 without the leading zero is a different number and will not give the result you expect. From PHP 8.1 the first argument is an FTP\Connection object.

Library declarations
PHP
ftp_chmod(FTP\Connection $ftp, int $permissions, string $filename): int|false

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
permissionsRequired. The new permissions as an octal integer, for example 0644 or 0755.
filenameRequired. The remote file or directory whose permissions you want to change.

Return Value

Return ValueDescription
int|falseThe new permissions as an integer on success, or false on failure.

PHP Examples

Example 1 — Set File Permissions

Change a remote file to 0644 and print the result. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$result = ftp_chmod($ftp, 0644, "index.php");

if ($result !== false) {
    echo "New permissions: ", decoct($result), PHP_EOL;
} else {
    echo "chmod failed.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. 0644 is an octal number: owner can read and write, others can only read.
  2. ftp_chmod() returns the new permissions as an integer.
  3. decoct() turns that integer back into an octal string for display.
  4. The result is compared with !== false to detect failure.

Example 2 — Forgetting the Leading Zero

The number 755 without a leading zero is decimal, not octal, so the permissions are not what you intended.

PHP
<?php
$wrong = 755;
$right = 0755;

echo "755 as octal:  ", decoct($wrong), PHP_EOL;
echo "0755 as octal: ", decoct($right), PHP_EOL;

// ftp_chmod($ftp, 755, "script.sh");   // wrong
// ftp_chmod($ftp, 0755, "script.sh");  // correct

How It Works

  1. PHP treats 755 as a normal decimal number.
  2. decoct() shows that 755 decimal is 1363 in octal, which is a strange permission set.
  3. 0755 with the leading zero is real octal and prints as 755.
  4. Always write 0755 when calling ftp_chmod().

Example 3 — Make a Folder and Set Its Permissions

Create a directory and give it the usual 0755 permissions in one script.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

$folder = "uploads";

if (ftp_mkdir($ftp, $folder) !== false) {
    $mode = ftp_chmod($ftp, 0755, $folder);
    echo "Created ", $folder, " with mode ", decoct($mode), PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_mkdir() creates the folder and returns its name, or false on failure.
  2. ftp_chmod() then sets the folder to 0755.
  3. The returned integer is shown in octal using decoct().
  4. The message confirms both steps worked.

Frequently Asked Questions

It changes the permissions of a remote file or folder on the FTP server using the SITE CHMOD command. It returns the new permissions as an integer.
The leading zero tells PHP the number is octal. Without it, 755 is a decimal number and gives completely different permissions.
Use 0644 for normal files and 0755 for folders and scripts. Avoid 0777 unless you really need it, because it lets everyone write to the file.
The server may not support SITE CHMOD, the file may not exist, or you may not have permission to change it.
Pass the returned integer to decoct(), for example decoct(ftp_chmod($ftp, 0644, 'file.txt')).

Next: ftp_close()

ftp_close() closes an open FTP connection. Call it when you have finished all your FTP wor.

ftp_close() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful