zoukankan      html  css  js  c++  java
  • PHP批量保存图片到服务器再上传阿里云

    /*
         * 批量传输产品主图到阿里云
         */
        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 '处理完成';
            }
            
        }
  • 相关阅读:
    MVC3基础嵌套总结
    List之Union(),Intersect(),Except() 亦可以说是数学中的并集,交集,差集
    sqlserver查询记录数某个区间内记录
    各语言的unix时间戳 【转】
    SQLserver删除某数据库中所有表
    C#日期格式转换大全
    【转】Linq之动态排序(字符传入)
    谈谈Equals和GetHashcode
    JS常用代码收集
    ROW_NUMBER()、RANK()、DENSE_RANK()、NTILE(N)
  • 原文地址:https://www.cnblogs.com/tdalcn/p/10043087.html
Copyright © 2011-2022 走看看