php 连接ftp上传,下载文件

public function actionFtp()
    {
        $ftp_ip = "";
        $ftp_user = "";
        $ftp_pwd = "";
        $path = 'fptfiles';//ftp服务器下的目录
        $putFilePath = 'D:\redislog.txt'; // 本地上传的文件
        $ftp_upfile = "redislog.txt"; //ftp上传保存的文件名
        $conn = ftp_connect($ftp_ip) or die("Could not connect");
        ftp_login($conn,$ftp_user,$ftp_pwd);
        //利用ftp创建目录
        $this->make_directory($conn,$path);
        //利用ftp选择进入目录
        ftp_chdir($conn,$path);

        $data = array();
        //开始上传  上传到了 ftp根目录下 fptfiles/attack.txt
        if(ftp_put($conn, $ftp_upfile,  $putFilePath , FTP_ASCII)){
            $data[] = "上传成功";
        }
        else{
            $data[] = "上传失败";
        }

        $getFilePath = 'D:\redislog2.txt';// 下载保存的文件路径
        $get_ftp_file = 'redislog2.txt'; //ftp上要下载的文件
        //ftp 下载
        if(ftp_get($conn, $getFilePath, $get_ftp_file, FTP_ASCII)){
            $data[] = "下载成功";
        }
        else{
            $data[] = "下载失败";
        }

        ftp_close($conn);
        //注意上传端的ftp权限设置
        $this->ajaxReturn(0,$data);
    }

    function make_directory($ftp_stream, $dir) {
        // if directory already exists or can be immediately created return true
        if ($this->ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir))
            return true;
        // otherwise recursively try to make the directory
        if (!$this->make_directory($ftp_stream, dirname($dir)))
            return false;
        // final step to create the directory
        return ftp_mkdir($ftp_stream, $dir);
    }

    function ftp_is_dir($ftp_stream, $dir) {
        // get current directory
        $original_directory = ftp_pwd($ftp_stream);
        // test if you can change directory to $dir
        // suppress errors in case $dir is not a file or not a directory
        if (@ftp_chdir($ftp_stream, $dir)) {
            // If it is a directory, then change the directory back to the original directory
            ftp_chdir($ftp_stream, $original_directory);
            return true;
        } else {
            return false;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值