码迷,mamicode.com
首页 > Web开发 > 详细

PHP批量保存图片到服务器再上传阿里云

时间:2018-11-30 14:01:52      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:https   code   replace   data   aliyun   产品   release   select   sql   

/*
     * 批量传输产品主图到阿里云
     */
    public function transferImage(){
        
        $num = 50;
        $p = isset($this->request->get[‘p‘])?$this->request->get[‘p‘]:0;
        $nonum = isset($this->request->get[‘nonum‘])?$this->request->get[‘nonum‘]:0;
        $start = ($nonum>0)?$nonum:$p*$num;
        
        $query = $this->db->query("SELECT product_id,image,site_id FROM " . DB_PREFIX . "product WHERE is_delete = 0 AND site_id=‘2170254929593344‘ AND image LIKE ‘%.alicdn.com%‘ LIMIT ".$start.",".$num);
        $result = $query->rows;
        
        //图片处理
        if(!empty($result)){

            $aliYunOSS = new AliYunOSS();

            foreach ($result as $key => $value){
                    
                $file = $value[‘image‘];
                
                //保存图片到服务器上
                $data = @file_get_contents($file);
                if(!empty($data)){
                    $ext = pathinfo($file, PATHINFO_EXTENSION);
                    $filename = $value[‘product_id‘].rand(). ‘.‘ . $ext;
                    $rootFile = DIR_UPLOAD .‘file/‘. $filename;
                    file_put_contents($rootFile, $data);
                     
                    //上传图片到阿里云
                    $fileDir = date(‘Y‘, time()) . ‘/‘ . date(‘m‘, time());
                    $ali = $aliYunOSS->uploadFile($rootFile, $value[‘site_id‘].‘/‘.$fileDir.‘/‘.$filename);
                    
                    if($ali[‘success‘]==1 && !empty($ali[‘info‘][‘url‘])){
                        $image = $ali[‘info‘][‘url‘];
                        
                        if (ENVIRONMENT == ‘pro‘) {
                            $image = str_replace(‘http://joinf-website-release.oss-cn-hongkong.aliyuncs.com‘, OSS_URL, $image);
                            $image = str_replace(‘https://joinf-website-release.oss-cn-hongkong.aliyuncs.com‘, OSS_URL, $image);
                        }
                        
                        $sql = "UPDATE " . DB_PREFIX . "product SET image=‘".$image."‘ WHERE product_id=‘".$value[‘product_id‘]."‘ AND image=‘".$value[‘image‘]."‘";
                        $this->db->query($sql);
                        
                        //删除服务器文件
                        unlink($rootFile);
                    }
                }else{
                    $nonum++;
                }
            }
            
            $url = $this->url->createUrl(‘test/transferImage‘,‘p=‘.($p+1).‘&nonum=‘.$nonum.‘&rand=‘.rand(1000, 9999));
            echo "<script>window.location.href=‘".$url."‘</script>";
            
        }else{
            echo ‘处理完成‘;
        }
        
    }

 

PHP批量保存图片到服务器再上传阿里云

标签:https   code   replace   data   aliyun   产品   release   select   sql   

原文地址:https://www.cnblogs.com/tdalcn/p/10043087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!