How can one troubleshoot and test FTP connections in PHP using a local development environment?

When troubleshooting FTP connections in PHP using a local development environment, you can start by checking if the FTP extension is enabled in your PHP configuration. You can also verify if the FTP server is running and accessible from your local machine. To test the FTP connection, you can try connecting to the FTP server using a simple PHP script that attempts to login and list the directory contents.

<?php
// Check if the FTP extension is enabled
if (!extension_loaded('ftp')) {
    die('FTP extension is not enabled.');
}

// FTP server credentials
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
if (!$conn_id) {
    die('Could not connect to FTP server');
}

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
    die('FTP login failed');
}

// List directory contents
$contents = ftp_nlist($conn_id, '/');
if ($contents) {
    foreach ($contents as $file) {
        echo $file . "\n";
    }
} else {
    echo 'Failed to list directory contents';
}

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