What are common issues when uploading files via FTP in PHP scripts?

Common issues when uploading files via FTP in PHP scripts include permissions errors, file size limitations, and connection timeouts. To solve these issues, ensure that the FTP server has the correct permissions set for file uploads, adjust PHP settings to allow for larger file uploads, and handle connection timeouts gracefully in your script.

// Example PHP code snippet for uploading a file via FTP with error handling

$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";
$local_file = "local_file.txt";
$remote_file = "remote_file.txt";

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

$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
    die("FTP login failed");
}

$upload_result = ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY);
if (!$upload_result) {
    die("Failed to upload file");
}

ftp_close($conn_id);
echo "File uploaded successfully";