How can PHP functions like ftp_connect and ftp_login be optimized for efficient FTP file transfers?

To optimize PHP functions like ftp_connect and ftp_login for efficient FTP file transfers, it is important to minimize the number of FTP commands sent to the server. This can be achieved by establishing a persistent FTP connection and reusing it for multiple file transfers. Additionally, setting passive mode for data transfers can help improve performance by reducing the overhead of establishing a new data connection for each file transfer.

// Connect to FTP server with persistent connection
$conn_id = ftp_connect('ftp.example.com', 21, 30);

// Login to FTP server
$login_result = ftp_login($conn_id, 'username', 'password');

// Set passive mode for data transfers
ftp_pasv($conn_id, true);

// Perform file transfer operations here

// Close FTP connection
ftp_close($conn_id);