首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP ftp_put函数文件限制?

PHP ftp_put函数文件限制?
EN

Stack Overflow用户
提问于 2019-03-19 16:09:05
回答 1查看 312关注 0票数 1

我想要将当前30k的文件发送到具有ftp_put功能的远程服务器。但页面超时,因为它试图将它们全部发送出去。我想在每次刷新页面时发送指定数量的文件。例如,每次刷新50个文件。有人能帮我吗?我搜索了很多次解决方案,但我找不到任何信息。我不知道怎么在网上搜索,因为我的英语不是很好,谢谢。

代码语言:javascript
运行
复制
<?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";
}

?>
EN

Stack Overflow用户

回答已采纳

发布于 2019-03-19 16:59:41

我最初的想法是获得critical_logs目录中的文件列表,并将它们记录到文件中。日志文件必须位于不同的目录中,以防止将其发送到FTP服务器。一旦文件列表存在,就以块的形式处理文件,在您的情况下,一次处理50个文件。处理完这50个文件后,将更新日志文件以删除这些条目-因此,在下一页加载时,该过程可以再次开始。已测试但未进行任何FTP测试的示例

代码语言:javascript
运行
复制
    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);
        }
    }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55236160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档