ftp_site()
The ftp_site() function sends a SITE command to an FTP server. Learn its syntax, usage, error handling, and how it compares to ftp_raw() and ftp_chmod().
Understanding the PHP Function ftp_site()
The ftp_site() function is a built-in PHP function that sends a SITE command to an FTP server. SITE is a catch-all FTP command for instructions that fall outside the standard FTP protocol — things each server vendor implements in its own way, such as changing file permissions, adjusting idle timeouts, or running server-specific maintenance tasks.
Because the meaning of a SITE command is defined entirely by the remote server, ftp_site() is the escape hatch you reach for when no dedicated PHP FTP function exists for what you need. This article explains what the function does, when to use it (and when not to), and how to handle its result safely.
To work with it you first open a connection with ftp_connect() and authenticate with ftp_login(). See the PHP FTP overview for the full family of functions.
What is ftp_site()?
The function takes two parameters:
ftp— The FTP connection returned byftp_connect()(orftp_ssl_connect()).command— The raw SITE command string to send, without the leadingSITEkeyword (PHP adds it for you).
It returns true if the server accepted the command and false on failure. A true result only means the server accepted the command — it does not guarantee the underlying action (for example, the permission change) succeeded, since SITE behavior is server-defined.
Syntax of ftp_site()
The syntax of the ftp_site() function is as follows:
Syntax of ftp_site()
ftp_site(FTP\Connection $ftp, string $command): boolAs of PHP 8.1 the first argument is an FTP\Connection object (in earlier versions it was a resource). The command argument is the SITE instruction to send — for example CHMOD 644 file.txt. You pass only the part after SITE; PHP prepends the keyword automatically.
Usage of ftp_site()
To use the ftp_site() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:
Usage of ftp_site()
<?php
// Set up an FTP connection
$conn = @ftp_connect('ftp.example.com');
if (!$conn) {
die('Could not connect to server.');
}
// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
die('Login failed.');
}
// Send the SITE command
ftp_site($conn, 'CHMOD 755 /public_html/index.php');
// Close the FTP connection
ftp_close($conn);In this example, we connect with ftp_connect(), authenticate with ftp_login(), send SITE CHMOD 755 to change the permissions of index.php, and finally close the connection with ftp_close().
Note: SITE commands are server-specific.
SITE CHMODis not universally supported across all FTP servers. For standard file-permission changes, prefer the dedicatedftp_chmod()function, which is portable and returns the new permission value. Reach forftp_site()only for actions that have no dedicated FTP function.
Common SITE commands
The exact set of supported commands depends on the server software (ProFTPD, vsftpd, Pure-FTPd, etc.), but a few are widely available:
| SITE command | What it does |
|---|---|
CHMOD 644 file.txt | Change file permissions (Unix-style octal mode). |
UMASK 022 | Set the default permission mask for newly created files. |
IDLE 600 | Set the connection idle timeout in seconds. |
HELP | Ask the server which SITE commands it supports. |
When in doubt, run SITE HELP to discover what a particular server accepts:
Discovering supported SITE commands
<?php
$conn = ftp_connect('ftp.example.com');
ftp_login($conn, 'username', 'password');
if (ftp_site($conn, 'HELP')) {
echo "Server accepted the SITE HELP command.\n";
}
ftp_close($conn);Error handling in ftp_site()
It's important to handle errors properly when using the ftp_site() function. If the function returns false, it means that the operation was unsuccessful. Here's an example of how to handle errors:
Error handling in ftp_site()
<?php
if (!ftp_site($conn, 'CHMOD 755 /public_html/index.php')) {
echo "Failed to send SITE command.\n";
}
ftp_close($conn);By checking the return value, you avoid silently continuing after a command the server rejected. Note that ftp_site() returns false both when the connection is bad and when the server simply does not recognise the command — so a false result is your cue to fall back to a dedicated function or a different approach.
ftp_site() vs. ftp_raw() and ftp_exec()
ftp_site() is closely related to two other "send a raw instruction" functions, and it helps to know when to pick each:
ftp_site()— sends a single SITE command and returns a boolean. Use it for server-specific actions likeCHMODorUMASK.ftp_raw()— sends any raw FTP command (not just SITE) and returns the server's full response as an array, so you can read the status code yourself. Use it when you need to inspect the reply.ftp_exec()— runs a program on the server viaSITE EXEC, where supported. Use it only on servers you control and trust.
For everyday operations (uploading, downloading, listing, renaming) prefer the purpose-built functions covered in the PHP FTP overview; they are more portable than hand-built SITE commands.
Conclusion
The ftp_site() function lets you send server-specific SITE commands when no dedicated PHP FTP function fits. Always check its return value, remember that a successful send is not the same as a successful action, and prefer portable functions like ftp_chmod() whenever one exists.