<?php
$file = 'critical_logs/'.$entry;
$send_to = 'httpdocs/abc/'.$entry;
$conn = ftp_connect('ftp.example.com');
if (!$conn) die('ftp.example.com connect error');
$login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn, $file, $send_to, FTP_ASCII)) {
echo "success\n";
} else {
echo "error\n";
}
?>
答案 0 :(得分:0)
我最初的想法是在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);
}
}