Skip to content
PHP Logo

In PHP scripts, how does the starting directory of ftp_connect() affect the path specified in functions like ftp_mkdir()?

When using FTP functions in PHP, the starting directory of ftp_connect() affects the path specified in functions like ftp_mkdir(). If the starting directory is not the root directory, you need to adjust the path accordingly by concatenating the starting directory with the desired path. This ensures that the function creates the directory in the correct location.

php
// Connect to FTP server
$conn = ftp_connect('ftp.example.com');

// Login to FTP server
ftp_login($conn, 'username', 'password');

// Set the starting directory
ftp_chdir($conn, '/path/to/starting/directory');

// Specify the desired directory path relative to the starting directory
$directory_path = 'new_directory';

// Concatenate the starting directory path with the desired path
$full_path = ftp_pwd($conn) . '/' . $directory_path;

// Create the directory
ftp_mkdir($conn, $full_path);

// Close the FTP connection
ftp_close($conn);