读取ftp的文件列表

    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 '
';
        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 '
'; } 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);
}

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

 

 

你可能感兴趣的:(每日总结,linux,php)