PHP批量保存图片到服务器再上传阿里云
Posted 它山之石可以攻玉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ‘处理完成‘; } }
以上是关于PHP批量保存图片到服务器再上传阿里云的主要内容,如果未能解决你的问题,请参考以下文章
ueditor1.4.3 php如何上传图片到图片服务器(七牛)