代码语言:javascript复制
use yiiwebUploadedFile;
代码语言:javascript复制public function actionUpload()
{
Yii::$app->response->format = Response::FORMAT_JSON;
if(Yii::$app->request->isPost) {
$image = UploadedFile::getInstanceByName('img');
$imageName = $image->getBaseName();
$ext = $image->getExtension();
$rootPath = 'assets/images/';
$path = $rootPath.date('Y/m/d/');
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
$fullName = $path.$imageName.$ext;
if($image->saveAs($fullName)) {
return ['code'=>1, 'message'=>'保存图片成功', 'data'=>$fullName];
} else {
return ['code'=>0, 'message'=>'保存图片失败', 'data'=>$image->error];
}
} else {
return ['code'=>0, 'message'=>'不是POST'];
}
}
原文链接:https://blog.csdn.net/lilongsy/article/details/84620377