What PHP function can be used to transfer files to a different server via FTP?
To transfer files to a different server via FTP in PHP, you can use the `ftp_put()` function. This function uploads a file to the specified FTP server. You need to establish a connection to the FTP server using `ftp_connect()` and then authenticate using `ftp_login()` before using `ftp_put()` to transfer the file.
// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$conn_id = ftp_connect($ftp_server);
// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
// File transfer via FTP
$file_path = 'local_file.txt';
$remote_file = 'remote_file.txt';
if (ftp_put($conn_id, $remote_file, $file_path, FTP_ASCII)) {
echo "File uploaded successfully";
} else {
echo "Failed to upload file";
}
// Close FTP connection
ftp_close($conn_id);
Related Questions
- What are potential pitfalls to be aware of when handling file uploads in PHP, such as checking for warnings or notices during the process?
- What potential pitfalls should be considered when using array_push to add entries to an array in PHP?
- How can PHP developers efficiently identify and fix SQL syntax errors in their code?