//模型 file->extension;//图片的名字只取后缀名 if(empty($text_name)){ $ran=time().rand(10000,99999);//随机名字 }else{ $ran = $text_name;} $arr = $arr_type; if(!empty($arr)){ if(!in_array($fill,$arr)){ return json_encode('格式不正确');} } //定义上传大小和类型 if(!empty($siez)){ if($img['size']>$siez) { return json_encode('文件大小超过限制');} } //文件上传存放的目录 $dir=$dirname.'/'.date("Ymd"); $dir_sev=$name.'/'.date("Ymd");/*数据库存储路径*/ if(!file_exists($dir)) { mkdir($dir,0777,true); } if ($this->validate()) { //文件名 $fileName = $ran .'.'.$fill; $dir = $dir."/". $fileName; if($act == 'update'){ if($this->file->saveAs($dir)){@unlink($path.$url);}}/*删除图片*/ $uploadSuccessPath = $dir_sev."/". $fileName;/*最后路径*/ return $uploadSuccessPath; } }}
//控制器 request->isPost) { $model->file = UploadedFile::getInstance($model, "file"); $file = $model->upload((array)$model->file); return $file; } return $this->render("index", [ "model" => $model, "uploadSuccessPath" => $uploadSuccessPath, ]); }}
//视图 ["enctype" => "multipart/form-data"]]);?>