把站点的图片存储到oss,取代图片服务器, 便于服务器扩展和后期站点集群。
目录结构上图:
上代码:
//上传项目图片到阿里oss服务器
function upload_file_to_alioss($data, $borrow_id)
{
$ossinfo = C("OSS_SERVERINFO");
import("ORG.Alioss.alioss");
$oss = new ALIOSS($access_id = $ossinfo['OSS_ACCESS_ID'], $access_key = $ossinfo['OSS_ACCESS_KEY'], $hostname = $ossinfo['OSS_ENDPOINT'], $security_token = NULL);
$bucket = $ossinfo['OSS_TEST_BUCKET'];
$arr = array();
foreach ($data as $v) {
$picname = 'borrow-' . $borrow_id .'-'. time() . rand(111111, 999999).'.jpg';
$arr[] = $picname;
$object = 'images/'.$picname;
$file_path = $_SERVER['DOCUMENT_ROOT'].$v;
$res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
Log::write('oss服务器返回信息========'.json_encode($res), Log::SQL);//查看调试信息
if ($res->status != 200) {
return false;
}
}
$ossupdata = serialize($arr);
$res = M("borrow_info")->where("id={$borrow_id}")->save(array('ossupdata' => $ossupdata));
if($res){
return true;
}else{
return false;
}