Yii2UploadedFile上传文件
use yiiwebUploadedFile;
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"];
}
}
————————————————
版权声明:本文为CSDN博主「lilongsy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lilongsy/article/details/84620377
以上是 Yii2UploadedFile上传文件 的全部内容, 来源链接: utcz.com/z/511017.html