读取ftp的文件列表

232 篇文章 0 订阅
140 篇文章 1 订阅
    public function getFtp()
    {
        $conn_id = ftp_connect('192.168.1.1');
        $login_result = ftp_login($conn_id, 'xxxx', 'xxxx123');
        if (!$login_result) {
            exit('login error');
        }
        ftp_pasv($conn_id, true); //配置被动模式,当服务器配置的是被动模式时须配置它,不然链接一次后再次刷新会返回false
        $contents = ftp_nlist($conn_id, "/home/mis/view/ACTFILE/");
//        ftp_get($conn_id);
//        ftp_quit($conn_id);
        echo '<pre />';
        var_dump($contents);
        ftp_close($conn_id);
        die;
        $files = [];

        foreach ($contents AS $content) {
            $ignoreArray = ['.', '..'];
            if (!in_array($content, $ignoreArray)) {
                $files[] = $content;
            }
        }

        var_dump($files);
    }
ftp_pasv($conn_id, true);

文件所在的服务器  配置的是被动模式  所以要切换到被动模式下连接     

ftp_pasv() 函数把被动模式设置为打开或关闭。

在被动模式中,数据连接是由客户机来初始化的,而不是服务器。

    public function getFtp()
    {
        $conn_id = ftp_connect('192.168.1.45');
        $login_result = ftp_login($conn_id, 'xxx', 'xx123');
        if (!$login_result) {
            exit('login error');
        }
        ftp_pasv($conn_id, true); //配置被动模式,当服务器配置的是被动模式时
//        ftp_chdir($conn_id, '');
//        $contents = ftp_nlist($conn_id, "/home/mis/view/ACTFILE/");

//        $files = [
//            '/home/mis/view/ACTFILE/AbcQR' . date('Ymd', strtotime('-1 day')) . '.txt',
//            '/home/mis/view/ACTFILE/Abc' . date('Ymd', strtotime('-1 day')) . '.txt'
//        ];

        $files = [
            '/home/mis/view/ACTFILE/AbcQR' . date('Ymd') . '.txt',
            '/home/mis/view/ACTFILE/Abc' . date('Ymd') . '.txt'
        ];

        foreach ($files as $kk => $vv) {
            $remote_path = $vv;
            $temp_path = tempnam(sys_get_temp_dir(), "ftp");
            //为避免必须为每个文件连接/登录,请使用ftp_get并重用您的连接ID($conn_id):
            ftp_get($conn_id, $temp_path, $remote_path, FTP_BINARY);
            $content = file_get_contents($temp_path);
            var_dump($content);
            echo '<br />';
        }
        ftp_close($conn_id);
    }

来源:

为避免必须为每个文件连接/登录,请ftp_get并重用您的连接ID($conn_id):

 

foreach ($files as $file)
{
    // Full path to a remote file
    $remote_path = "DirectoryName/$file";
    // Path to a temporary local copy of the remote file
    $temp_path = tempnam(sys_get_temp_dir(), "ftp");
    // Temporarily download the file
    ftp_get($conn_id, $temp_path, $remote_path, FTP_BINARY);
    // Read the contents of temporary copy
    $contents = file_get_contents($temp_path);
    $content[$file] = $contents;
    // Discard the temporary copy
    unlink($temp_path);
}

(您应该添加一些错误检查.)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值