How can the FTP connection and file transfer process be optimized for better performance in PHP?
To optimize FTP connection and file transfer performance in PHP, you can use passive mode for FTP connections, limit the number of simultaneous connections, and batch file transfers to reduce overhead.
// Optimize FTP connection and file transfer performance
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$ftp_connection = ftp_connect($ftp_server);
ftp_login($ftp_connection, $ftp_username, $ftp_password);
// Set passive mode for FTP connection
ftp_pasv($ftp_connection, true);
// Limit the number of simultaneous connections
ftp_set_option($ftp_connection, FTP_USEPASVADDRESS, false);
ftp_set_option($ftp_connection, FTP_TIMEOUT_SEC, 10);
// Batch file transfers to reduce overhead
$files = ['file1.txt', 'file2.txt', 'file3.txt'];
foreach ($files as $file) {
ftp_put($ftp_connection, $file, $file, FTP_BINARY);
}
// Close FTP connection
ftp_close($ftp_connection);