[英]PHP ftp_put function file limitation?
我想將當前的30k文件發送到具有ftp_put功能的遠程服務器。 但是頁面超時,因為它試圖將所有內容發送出去。 我想在每次刷新頁面時發送指定數量的文件。 例如,每次刷新50個文件。 有人可以幫我嗎? 我搜索了很多解決方案,但找不到任何信息。 我不太清楚如何在互聯網上搜索,因為我的英語不太好,謝謝。
<?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";
}
?>
我最初的想法是獲取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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.