How can PHP be used to upload a file to an FTP server and verify that it has been successfully uploaded?
To upload a file to an FTP server using PHP, you can use the `ftp_put()` function. After uploading the file, you can verify its successful upload by checking if the file exists on the server. You can do this by using the `ftp_nlist()` function to get a list of files on the server and then checking if the uploaded file is in the list.
// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$conn_id = ftp_connect($ftp_server);
$login = ftp_login($conn_id, $ftp_username, $ftp_password);
// Upload file to FTP server
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}
// Verify file upload
$files = ftp_nlist($conn_id, ".");
if (in_array($remote_file, $files)) {
echo "File successfully uploaded to FTP server.";
} else {
echo "File upload verification failed.";
}
// Close FTP connection
ftp_close($conn_id);