ftp_mkdir()
The ftp_mkdir() function is a PHP built-in function that creates a new directory on the FTP server. The function takes two parameters:
What is ftp_mkdir()?
The ftp_mkdir() function is a PHP built-in function that creates a new directory on the FTP server. The function takes two parameters:
- ftp_stream: The connection identifier returned by the ftp_connect() function.
- directory: The name of the directory to create.
The function returns true if the directory was created successfully. Otherwise, it returns false. Note that ftp_mkdir() cannot create nested directories; intermediate paths must be created manually.
Syntax of ftp_mkdir()
The syntax of the ftp_mkdir() function is as follows:
Syntax of ftp_mkdir()
bool ftp_mkdir ( FTP\Connection $ftp_stream , string $directory )Note: The typed signature above requires PHP 8.0+. The function is available since PHP 4.3.0.
Usage of ftp_mkdir()
To use the ftp_mkdir() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:
Usage of ftp_mkdir()
<?php
// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');
// Login with your FTP credentials
ftp_login($conn, 'username', 'password');
// Create a new directory
if (ftp_mkdir($conn, '/path/to/new/directory')) {
echo "Directory created successfully.\n";
} else {
echo "Failed to create directory.\n";
}
// Close the FTP connection
ftp_close($conn);In this example, we establish a connection to the FTP server using the ftp_connect() function. Then we log in using our FTP credentials using the ftp_login() function. Finally, we create a new directory using the ftp_mkdir() function and close the FTP connection.
Error handling in ftp_mkdir()
It's important to handle errors properly when using the ftp_mkdir() function. If the function returns false, it means that the directory creation was unsuccessful. Common reasons include insufficient permissions, an invalid path, or the target directory already existing. Note that ftp_mkdir() will fail if the specified directory already exists.
Here's an example of how to handle errors and configure reliable FTP settings:
Error handling in ftp_mkdir()
<?php
if (ftp_mkdir($conn, '/path/to/new/directory')) {
echo "Directory created successfully.\n";
} else {
echo "Failed to create directory.\n";
// Note: PHP's FTP extension does not expose the last server response directly.
// Verify connection status, permissions, and path validity.
}
?>For reliable FTP operations, consider setting a timeout and enabling passive mode before creating directories:
ftp_set_option($conn, FTP_TIMEOUT_SEC, 30);
ftp_set_option($conn, FTP_USEPASVADDRESS, true);Conclusion
The ftp_mkdir() function provides a straightforward way to create directories on an FTP server. Remember to validate your connection, handle errors gracefully, set appropriate timeouts, and create intermediate directories manually when needed. Applying these practices will help you integrate FTP directory management into your PHP applications reliably.
Practice
What is the primary function of the ftp_mkdir() in PHP?