Are there any best practices or alternative methods to improve the efficiency of FTP file uploads in PHP scripts to avoid time limit exceeded errors?
When uploading large files via FTP in PHP scripts, the process may take longer than the default time limit set in PHP. To avoid "time limit exceeded" errors, you can increase the time limit for the script or break the file into smaller chunks for uploading.
// Increase the time limit for the script
set_time_limit(0);
// Example of uploading a file in chunks
$localFile = 'path/to/local/file.txt';
$remoteFile = 'path/to/remote/file.txt';
$chunkSize = 1024; // 1KB chunks
$handle = fopen($localFile, 'rb');
$connection = ftp_connect('ftp.example.com');
ftp_login($connection, 'username', 'password');
while (!feof($handle)) {
$chunk = fread($handle, $chunkSize);
ftp_fput($connection, $remoteFile, $chunk, FTP_BINARY);
}
fclose($handle);
ftp_close($connection);