public function actionAdd(){ $img_banner = trim($_FILES['vend_banner']['name']); $img_banner = explode('.',$img_banner); $imgs_banner = $img_banner[1]; $img_banner = date('YmdHis').mt_rand(100,1000).md5($img_banner[0]); //文件重命名 $vend_banners = "./images/upload/".$img_banner.'.'.$imgs_banner; $vend_banner = $img_banner.'.'.$imgs_banner; $uploadPath = dirname(dirname(__FILE__)).'/web/images/upload/'; // 取得临时文件路径 if (!file_exists($uploadPath)) { @mkdir($uploadPath, 0777, true); } $file_Path_vend_banner = $uploadPath.$vend_banner; $filepath_vend_banner=str_replace("\\", "https://www.jb51.net/",$file_Path_vend_banner);//绝对路径,上传第二个参数 $object_vend_banner="data/Company/".$vend_banner; //拼接存储路径和文件名称,上传第一个参数 if(is_uploaded_file($_FILES['vend_banner']['tmp_name'])){ //判断是否post上传 if(!move_uploaded_file($_FILES['vend_banner']['tmp_name'],$vend_banners)){ //移动到临时目录里 echo 'banner上传失败';exit(); } } $vend_banner_url = Yii::$app->Aliyunoss->upload($object_vend_banner,$filepath_vend_banner); //调用新建的文件,执行OSS上传,返回的是上传到阿里云的OSS文件路径,打印出来是: //string(116) "http:// ******.oss-cn-******.aliyuncs.com/data/Company/20170908124236303cc17c30cd111c7215fc8f51f8790e0e1.jpg"
11、删除图片
$oss_url=$data2[0]['vend_banner'];//只要(上传时候的)图片名, $object="data/Company/".$oss_url; //将你阿里云的存储路径+图片名=组成数据 $is_del = Yii::$app->Aliyunoss->delete($object);//删除指定的图片,返回true,否则返回false