PHP ftp_raw() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_raw() sends a raw FTP command to the server and gives you the reply as an array of lines.

Most tasks have a ready-made function such as ftp_mkdir() or ftp_size(). When you need a command that PHP does not wrap, like FEAT or NOOP, ftp_raw() lets you send it yourself.

Syntax

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

PHP
$reply = ftp_raw($ftp, 'NOOP');
CallWhat it does
ftp_raw($ftp, 'NOOP')Sends a harmless no-operation command, often used to keep a connection alive.
ftp_raw($ftp, 'PWD')Sends the PWD command and returns the server reply, for example 257 "/" is the current directory.
ftp_raw($ftp, 'FEAT')Asks the server which extra features it supports.

ftp_raw() does not interpret the reply for you. You get the plain text lines from the server, and the first three characters of each line are the FTP status code, such as 200 or 550.

Library declarations
PHP
ftp_raw(FTP\Connection $ftp, string $command): ?array

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should be logged in first.
commandRequired. The raw FTP command to send, for example NOOP or PWD.

Return Value

Return ValueDescription
array|nullReturns the server reply as an array of lines. It can return null if the command could not be sent.

PHP Examples

Example 1 — Send a NOOP Command

Send the NOOP command and print the reply from the server.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$reply = ftp_raw($ftp, 'NOOP');

print_r($reply);

ftp_close($ftp);

How It Works

  1. After logging in, ftp_raw() sends the text NOOP to the server.
  2. NOOP means do nothing, so the server only confirms it is still alive.
  3. The reply comes back as an array with one line of text.
  4. The number 200 at the start of the line is the FTP success code.

Example 2 — Read the Status Code

Check the first three characters of the reply to see whether the command worked.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$reply = ftp_raw($ftp, 'PWD');
$code  = (int) substr($reply[0], 0, 3);

if ($code === 257) {
    echo "OK: ", $reply[0], PHP_EOL;
} else {
    echo "Unexpected code: $code", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_raw() sends the PWD command and returns the reply lines.
  2. substr() takes the first three characters, which is the status code.
  3. The code 257 means the directory was reported successfully.
  4. The script prints the full reply when the code is what we expected.

Example 3 — Ask the Server for Features

Use the FEAT command to list the extra features a server supports.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

$reply = ftp_raw($ftp, 'FEAT');

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

ftp_close($ftp);

How It Works

  1. FEAT is a standard command that asks the server what it supports.
  2. ftp_raw() returns every line of the answer as an array item.
  3. foreach goes through each line and trim() removes extra spaces.
  4. The list shows features like MDTM and SIZE, which match PHP functions such as ftp_mdtm() and ftp_size().

Frequently Asked Questions

It sends any raw FTP command to the server and returns the reply as an array of text lines. It is useful for commands PHP does not have a function for.
Each line starts with a three digit status code. For example 200 means success and 550 means the action was refused. Read the code first, then the message.
No, avoid it. Passing user input directly could let someone run unwanted FTP commands. Always validate or whitelist the command first.
Use the dedicated functions like ftp_mkdir(), ftp_delete() or ftp_size() whenever they exist. Use ftp_raw() only when no matching PHP function is available.
ftp_raw() sends any command and returns the reply, while ftp_site() sends a SITE command and only returns true or false.

Next: ftp_rawlist()

ftp_rawlist() returns a detailed list of files in a remote folder. Each item is one line o.

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