What are the advantages and disadvantages of using FTP for image uploads in PHP?

When uploading images in PHP, using FTP can be advantageous because it allows for direct transfer of files to a server, which can be faster and more reliable than other methods. However, FTP may require additional setup and configuration, and it may not be as secure as other methods like using a form with file input.

<?php
// Example code for uploading an image using FTP in PHP

$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$remote_file = "/path/to/upload/image.jpg";
$local_file = "image.jpg";

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

// Upload file
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
    echo "Successfully uploaded image.";
} else {
    echo "Error uploading image.";
}

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