PHP ftp_exec() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_exec() asks the FTP server to run a command using the SITE EXEC feature.

It is a rarely used function. Most servers disable SITE EXEC for security reasons, so ftp_exec() often returns false. It is not the same as running a command in a shell on your own machine.

Syntax

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

PHP
$ok = ftp_exec($ftp, "ls -la");
CallWhat it does
ftp_exec($ftp, "ls -la")Asks the server to run the command and returns true or false.
ftp_exec($ftp, "touch ready.txt")Tries to create a file by running a command on the server.
ftp_exec($ftp, "uptime")Runs a harmless command if the server allows SITE EXEC.

From PHP 8.1 the first argument is an FTP\Connection object. The function only reports whether the command was accepted. It does not give you the command output. Use ftp_raw() if you need the server reply text.

Library declarations
PHP
ftp_exec(FTP\Connection $ftp, string $command): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
commandRequired. The command string to send with SITE EXEC.

Return Value

Return ValueDescription
booltrue if the server accepted the command (a 2xx reply), otherwise false.

PHP Examples

Example 1 — Send a Command to the Server

Try to run a command on the server and report the result. The host and login are placeholders.

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

if (ftp_exec($ftp, "touch ready.txt")) {
    echo "Command accepted by the server.", PHP_EOL;
} else {
    echo "Command was refused.", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. We connect and log in.
  2. ftp_exec() sends SITE EXEC touch ready. txt to the server.
  3. Most servers disable this feature, so false is the common answer.
  4. The script prints a message that matches the result.

Example 2 — Read the Reply With ftp_raw()

ftp_exec() only returns true or false. If you want to see why, use ftp_raw() to read the server reply.

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

$reply = ftp_raw($ftp, "SITE EXEC uptime");

foreach ($reply as $line) {
    echo trim($line), PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_raw() sends any FTP command and returns the reply lines as an array.
  2. We send the same SITE EXEC command in raw form.
  3. The server replies with an error code and a message.
  4. We print each line to learn why ftp_exec() would have returned false.

Example 3 — Fallback When Exec Is Not Allowed

Write code that does not break when the server refuses the command.

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

$allowed = @ftp_exec($ftp, "echo hello");

echo $allowed
    ? "SITE EXEC works here."
    : "SITE EXEC not available, using normal FTP only.";

echo PHP_EOL;

ftp_close($ftp);

How It Works

  1. We call ftp_exec() with a harmless command.
  2. The @ operator hides any warning from PHP.
  3. Because the result is false, the else part of the ternary runs.
  4. Your script can continue using normal FTP functions only.

Frequently Asked Questions

It sends the SITE EXEC command to the FTP server so the server can run a command. It returns true if the server accepts it.
Most FTP servers disable SITE EXEC because it is a security risk. The server replies with an error, and PHP returns false.
No. It only returns true or false. Use ftp_raw() with SITE EXEC if you want to read the server reply text.
No. Never pass user input into it, because it could let someone run commands on your server.
Use SSH or a proper web API for running server tasks. FTP is meant for transferring files, not running commands.

Next: ftp_fget()

ftp_fget() downloads a file from the FTP server and writes it into an open local file hand.

ftp_fget() 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