编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Yii2 UploadedFile上传文件(upload the file)

wxchong 2024-09-11 10:49:29 开源技术 8 ℃ 0 评论
use yii\web\UploadedFile;

public function actionUpload()
{
Yii::$app->response->format = Response::FORMAT_JSON;
if(Yii::$app->request->isPost) {
$image = UploadedFile::getInstanceByName('img');
imageName =
imageName=image->getBaseName();
ext =
ext=image->getExtension();
$rootPath = 'assets/images/';
path =
path=rootPath.date('Y/m/d/');
if (!file_exists($path)) {
mkdir($path, 0755, true);
}
fullName =
fullName=path.imageName.imageName.ext;
if(image->saveAs(
image?>saveAs(fullName)) {
return ['code'=>1, 'message'=>'保存图片成功', 'data'=>$fullName];
} else {
return ['code'=>0, 'message'=>'保存图片失败', 'data'=>$image->error];
}
} else {
return ['code'=>0, 'message'=>'不是POST'];
}
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表