PHP ftp_mkdir() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_mkdir() creates a new directory on an FTP server. On success it returns the name of the directory that was created.

It works like the mkdir command on your computer, but the folder is created on the remote server. It makes only one level at a time, so you need to create parent folders first if they do not exist.

Syntax

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

PHP
$dir = ftp_mkdir($ftp, 'uploads');
CallWhat it does
ftp_mkdir($ftp, 'uploads')Creates uploads in the current remote directory and returns its name.
ftp_mkdir($ftp, '/public_html/images')Creates a directory using an absolute remote path.
@ftp_mkdir($ftp, 'logs')Hides the warning so you can check for false yourself.

ftp_mkdir() is not recursive. Creating a/b/c fails if a/b is missing. It returns false and raises a warning if the directory already exists or you lack permission.

Library declarations
PHP
ftp_mkdir(FTP\Connection $ftp, string $directory): string|false

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
directoryRequired. The name or path of the directory to create on the server.

Return Value

Return ValueDescription
stringThe name of the newly created directory on success.
falseReturned when the directory could not be created.

PHP Examples

Example 1 — Create a Remote Folder

Log in and create a folder named uploads on the server.

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

$dir = ftp_mkdir($ftp, 'uploads');

if ($dir !== false) {
    echo "Created: ", $dir, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The script connects and logs in first, because ftp_mkdir() needs a logged-in session.
  2. ftp_mkdir() asks the server to create the uploads directory.
  3. The returned name is stored in $dir.
  4. The strict check !== false confirms the folder was created.

Example 2 — Handle an Existing Directory

If the folder already exists, ftp_mkdir() fails. Check the result and carry on.

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

$result = @ftp_mkdir($ftp, 'uploads');

if ($result === false) {
    echo "Could not create folder (it may already exist)", PHP_EOL;
} else {
    echo "Folder created", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The uploads folder was already created earlier.
  2. ftp_mkdir() returns false because the server refuses to create a duplicate.
  3. The @ operator keeps the warning message off the page.
  4. The if statement prints a helpful message instead.

Example 3 — Create Nested Folders One by One

Because ftp_mkdir() is not recursive, build a path piece by piece.

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

$path = 'media/2026/november';
$current = '';

foreach (explode('/', $path) as $part) {
    $current .= ($current === '' ? '' : '/') . $part;
    @ftp_mkdir($ftp, $current);
    echo "Ready: ", $current, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. explode() splits the path into media, 2026 and november.
  2. $current grows by one part on each loop pass.
  3. ftp_mkdir() creates each level, and @ ignores errors for folders that already exist.
  4. The final folder is ready to receive uploads.

Frequently Asked Questions

It creates a new directory on an FTP server and returns the name of the directory on success. If it fails, it returns false.
No. It creates only one level at a time. Create each parent folder first, or loop through the parts of the path.
The folder may already exist, the parent folder may be missing, or your account may not have write permission.
Call ftp_chmod() after ftp_mkdir(), for example ftp_chmod($ftp, 0755, 'uploads').
Use ftp_rmdir(). The directory must be empty before the server lets you delete it.

Next: ftp_mlsd()

ftp_mlsd() returns a list of the files and folders inside a remote directory. Each item is.

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