Skip to content
PHP Logo

In the provided PHP script, what steps are taken to establish the FTP connection before attempting to create a folder?

Issue: Before attempting to create a folder using FTP in PHP, it is essential to establish a connection to the FTP server. Solution: To establish an FTP connection before creating a folder, you need to use the `ftp_connect()` function to connect to the FTP server. After establishing the connection, you can use the `ftp_login()` function to authenticate with the FTP server using the provided username and password.

php
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "your-ftp-username";
$ftp_password = "your-ftp-password";

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if ($login_result) {
    echo "Connected and logged in to FTP server successfully.";
    // Now you can proceed with creating a folder using ftp_mkdir() function
} else {
    echo "Failed to connect or login to FTP server.";
}

// Close FTP connection
ftp_close($conn_id);
?>