What are the advantages and disadvantages of using FTP functions in PHP for file uploads compared to using copy()?
When uploading files in PHP, using FTP functions allows for more control and flexibility over the upload process, such as setting permissions and handling errors. However, using copy() is simpler and more straightforward for basic file uploads.
// Using FTP functions for file upload
$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";
$local_file = "local-file.txt";
$remote_file = "remote-file.txt";
// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pass);
// Upload file
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "File uploaded successfully";
} else {
echo "Error uploading file";
}
// Close FTP connection
ftp_close($conn_id);