ftp_raw()
The ftp_raw() function is a built-in PHP function that sends an arbitrary command to an FTP server. In this article, we'll discuss the function in detail and
Understanding the PHP Function ftp_raw()
The ftp_raw() function is a built-in PHP function that sends an arbitrary command directly to an FTP server and returns the server's raw, unparsed response. Unlike the higher-level FTP helpers, ftp_raw() does not interpret the result for you — it hands back exactly what the server said, line by line. This page explains what the function does, when you actually need it, and how to use it safely.
What is ftp_raw()?
FTP is a text-based protocol: clients send short commands like SYST, PWD, or FEAT, and the server replies with numbered status lines (for example 215 UNIX Type: L8). Most PHP FTP functions — such as ftp_nlist(), ftp_get(), and ftp_systype() — wrap one of these commands and parse the reply into a convenient value. ftp_raw() skips the parsing and exposes the protocol directly.
It takes two parameters:
ftp_stream— the connection identifier returned byftp_connect().command— the FTP command string to send (for example'SYST'or'PWD').
On success it returns an array of strings, one element per response line, containing the server's reply. On failure (for example an invalid connection) it returns false.
When should you use it?
For everyday tasks — listing, uploading, downloading — prefer the dedicated functions; they handle data connections and parsing for you. Reach for ftp_raw() only when:
- You need a command PHP has no wrapper for (for example
FEATto discover server features, or a customSITEdirective). - You are debugging and want to see the exact status line the server returns.
- You are implementing protocol behavior that the high-level API doesn't expose.
Note that ftp_raw() is meant for control-channel commands only. It cannot transfer file data — commands like RETR or LIST need a separate data connection, which is why you use ftp_get() or ftp_nlist() for those instead.
Common raw commands
| Command | Purpose | High-level alternative |
|---|---|---|
SYST | Report the server's operating system | ftp_systype() |
PWD | Print the current working directory | ftp_pwd() |
FEAT | List extended features the server supports | (none) |
NOOP | Keep-alive ping that does nothing | (none) |
STAT | Return server/connection status | (none) |
Syntax of ftp_raw()
The signature of the ftp_raw() function is as follows:
ftp_raw(FTP\Connection $ftp, string $command): arrayThe $ftp parameter is the connection returned by ftp_connect(), and $command is the command string to send. Both are required.
Usage of ftp_raw()
To use the ftp_raw() function, you first need to establish a connection to the FTP server with ftp_connect() and authenticate with ftp_login(). Here's a complete example:
<?php
// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
if (!$conn) {
die("Could not connect to FTP server.\n");
}
// Login with your FTP credentials
if (!ftp_login($conn, 'username', 'password')) {
die("Login failed.\n");
}
// Ask the server what operating system it runs
$response = ftp_raw($conn, 'SYST');
// Output the server's raw response, one line per element
echo "Server response: " . implode("\n", $response) . "\n";
// e.g. Server response: 215 UNIX Type: L8
// Close the FTP connection
ftp_close($conn);Here we connect with ftp_connect() and verify it succeeded, authenticate with ftp_login(), send the SYST command with ftp_raw(), and print each response line. The 215 is the FTP reply code; the text after it is the server's answer. Finally we release the connection with ftp_close().
Error handling in ftp_raw()
Always check the return value before using it. ftp_raw() returns false when it cannot send the command (for example because the connection is no longer valid), so passing that result straight into implode() would trigger a type error. Guard against it:
<?php
$response = ftp_raw($conn, 'SYST');
if ($response === false) {
echo "Failed to send the raw command to the FTP server.\n";
} else {
echo implode("\n", $response) . "\n";
}
ftp_close($conn);Keep in mind that a non-false array does not guarantee the command itself succeeded — it only means the server replied. Inspect the reply code (the leading number on each line, such as 5xx for errors) when you need to know whether the command was accepted.
Conclusion
The ftp_raw() function gives you direct access to the FTP control channel, which is invaluable for commands PHP doesn't wrap (like FEAT) and for debugging. For routine listing, uploading, and downloading, reach for the dedicated functions instead. See the full PHP FTP function reference for the complete set.