Первоначально у меня была идея получить список файлов в каталоге critical_logs
и записать их в файл. Файл журнала должен находиться в другом каталоге, чтобы предотвратить его отправку на FTP-сервер. Как только этот список файлов существует, обрабатывайте файл кусками, в вашем случае, по 50 за раз. Как только 50 файлов были обработаны, файл журнала обновляется для удаления этих записей - таким образом, на следующей странице загрузки процесс может начаться снова. Пример, проверенный, но без какого-либо FTP-тестирования
set_time_limit(0);
error_reporting( E_ALL );
$chunksize=20; # set to 50
$use_ftp_cmds=false; # set as true to actually attempt FTP commands
$ftp_host='ftp.example.com';
$ftp_send_to='httpdocs/abs/';
/* find all files in directory */
$dir = __DIR__ . '/critical_logs/';
$files = glob( $dir . '*.*' );
/* create, if it does not exist, a log to record the files - in parent directory */
chdir('..\\');
$log = getcwd() . '/files.txt';
/* add record for each file */
if( !file_exists( $log ) ){
foreach( $files as $file ){
file_put_contents( $log, $file . PHP_EOL, FILE_APPEND );
}
}
/* process the file in chunks */
if( file_exists( $log ) ){
clearstatcache();
$lines = file( $log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
if( count( $lines ) > 0 ){
$chunks = array_chunk( $lines, $chunksize );
/* connect to FTP server */
if( $use_ftp_cmds ){
$conn = ftp_connect( $ftp_host );
if( !$conn ) die( sprintf( '%s connect error', $ftp_host ) );
$login_result = ftp_login( $conn, $ftp_user_name, $ftp_user_pass );
if( !$login_result ) die( 'Failed to login to FTP server' );
}
/* Process the first chunk */
$chunk = array_shift( $chunks );
foreach( $chunk as $file ){
/* ftp cmds */
$result = $use_ftp_cmds ? ftp_put( $conn, $file, sprintf( '%s/%s', $ftp_send_to, $file ) , FTP_ASCII ) : true;
if( $result ){
/* remove the line from the source file and save updated version */
array_splice( $lines, array_search( $file, $lines ), 1 );
}
}
file_put_contents( $log, '' );
foreach( $lines as $line ){
file_put_contents( $log, $line . PHP_EOL, FILE_APPEND );
}
} else {
printf('The file %s is empty',$log);
}
}