php 下载文件 解压文件 上传文件

//总流程
public function download_demo_file(){
    //1.下载
    $url = 'http:/www.xxx.com/uploads.zip';
    $file = file_get_contents($url);
    $path = 'xxx';//要存放的文件地址
    $fileName = '/uploads.zip';
    //2.保存到线上或者本地地址
    file_put_contents($path.$fileName,$file);
    //3.解压
    $this->unZip($path , $path.$fileName);
    //4.上传到另外站点(上传解压后的文件目录级文件夹)
    $this->get_file_list($path);
}
// 解压文件
public function unZip($dir, $zipfile)
{
    exec('unzip -o -d ' . $dir . ' ' . $zipfile);
    return true;
}
//处理文件目录上传
public function get_file_list($path){
    $file_list = [];
    $file_path = $path;
    if (is_dir($file_path)){
        $handler = opendir($file_path);
        while( ($filename = readdir($handler)) !== false ) {
                if ($filename == "." || $filename == "..") {
                    continue;
                }
                if(is_dir($file_path.'/'.$filename)){
                    $file_list[] = $this->get_file_list($file_path.'/'.$filename);
                }elseif (is_file($file_path.'/'.$filename)){
                    $file_list[] = $file_path.'/'.$filename;
                    $this->uploadFile([$file_path.'/'.$filename], false);
                }
            }
        closedir($handler);
        return $file_list;
    }
}
//多文件上传
public function uploadFile($files=[],$type=true)
    {
        if (is_array($files))
        {
            foreach ($files as $key=>$file)
            {
                if($type)
                {
                    $this->dir_mkdirs($file);
                }
                $this->off = @ftp_put($this->ftp,$file,$key,FTP_BINARY);
                if(!$this->off)
                {
                     logs('ftp.txt',date('Y-m-d H:i:s').$file."文件上传错误");
                }else{
                    //删除文件
                    unlink($key);
                   // return true;
                }
            }
        }
        if(!$this->off)
        {
            //logs函数自定义日志
            logs('ftp.txt',date('Y-m-d H:i:s').$file."文件上传错误");
            return false;
        }else{
            return true;
        }
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值