Yii2 实现上传到本地及云 [ 2.0 版本 ]
_form.php
<?php $form = ActiveForm::begin([ 'options' => [ 'enctype' => 'multipart/form-data' ]]); ?>//上传图片必须设置的参数
<?php
echo $form->field($model, 'logo_url')->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
] );
?>
控制器
public function qiniu($imageUploadFile){
$typeUrl = 'event/';
$helper = new Helper();
$saveUrl = $helper->saveImage($imageUploadFile, '', '', $typeUrl);
// var_dump($saveUrl);exit;
$imageUrl = Yii::$app->getBasePath() .'/'.$saveUrl;
$ret = $helper->qiniu($imageUrl,$saveUrl);
// Yii::error( json_encode("错误:".$ret,JSON_UNESCAPED_UNICODE) ) ;
unlink($imageUrl);
if(!isset($ret['key']) || empty($ret['key'])){
throw new LogicErrorHttpException("上传图片失败", $this->error);
}
return $saveUrl;
}
/**
* Creates a new EventInfo model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new EventInfo();
$model->user_info_id = 312;
if ($model->load(Yii::$app->request->post())) {
$this->setFormatData($model, Yii::$app->request->post());
//var_dump($_FILES);exit;
$imageUploadFile = UploadedFile::getInstance($model,'logo_url');
if($imageUploadFile != null ){
//$imageUploadFile = UploadedFile::getInstanceByName('EventInfo[logo_url]');
$saveUrl = $this->qiniu($imageUploadFile);
$model->logo_url = $saveUrl;
}
$mPicUrlInstance = UploadedFile::getInstance($model,'m_pic_url');
if($mPicUrlInstance != null ){
// $imageUploadFile = UploadedFile::getInstanceByName('EventInfo[m_pic_url]');
$saveUrl = $this->qiniu( $mPicUrlInstance );
$model->m_pic_url = $saveUrl;
}
if ($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
]);
}
}
else {
return $this->render('create', [
'model' => $model,
]);
}
}
helper
/**
* 通过url上传图片到七牛
* @param $profileImage
* @param $userPhoto
*/
public function qiniu($profileImageUrl,$userPhoto){
$imageUrl = (string)$profileImageUrl;//头像地址链接
$imageName = (string)$userPhoto;//图片文件拼接名称,如:user_photo/2015/01/30/1422587704_640x194.png
//七牛的密钥、域名和空间
$ak = Yii::$app->params['ak'];
$sk = Yii::$app->params['sk'];
$domain = Yii::$app->params['qiniu_domain'];
$bucket = Yii::$app->params['qiniu_bucket'];
$qiNiu = new Qiniu($ak, $sk,$domain, $bucket);//登入到七牛
$ret = $qiNiu->uploadByUrl($imageUrl,$imageName);
//$url = $qiNiu->getLink($imageName);//获取已上传到七牛的图片url
return $ret;
}
/**
*
* @param type $imageUploadInstance UploadedFile::getInstance
* @return string | null
*/
public static function saveImage($imageUploadInstance, $width, $height, $type)
{
if ($imageUploadInstance == null)
{
return null;
}
$imageFileExt = strtolower($imageUploadInstance->getExtension());
$save_path = Yii::$app->getBasePath().'/'.$type;
if (!file_exists($save_path))
{
mkdir($save_path, 0777, true);
}
$ymd = date("Y/md");
$save_path .= $ymd . '/';
if (!file_exists($save_path))
{
mkdir($save_path, 0777, true);
}
$img_prefix = date("YmdHis") . '_' . rand(10000, 99999);
$imageFileName = $img_prefix . '.' . $imageFileExt;
$save_path .= $imageFileName;
$imageUploadInstance->saveAs($save_path);
//$obj = Image::thumbnail($save_path, $width, $height)->save($save_path);
return $type.$ymd . '/'. $imageFileName;
}
蛋黄派
注册时间:2015-02-27
最后登录:2017-01-19
在线时长:10小时36分
最后登录:2017-01-19
在线时长:10小时36分
- 粉丝10
- 金钱240
- 威望60
- 积分940
共 1 条评论
你这个命名一会儿驼峰一会儿下划线我也是醉了。