yii2利用自带UploadedFile上传图片
public static function uploadFile($name) { $uploadedFile = UploadedFile::getInstanceByName($name); if($uploadedFile === null || $uploadedFile->hasError) { return null; } $ymd = date("Ymd"); $save_path = dirname(Yii::$app->basePath).'\\web\\upload\\images\\'. $ymd . "\\"; $save_url = dirname(Yii::$app->homeUrl).'/upload/images/' . $ymd . "/"; if(! file_exists($save_path)) { mkdir($save_path); } $file_name = $uploadedFile->getBaseName(); $file_ext = $uploadedFile->getExtension(); // 新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; $uploadedFile->saveAs($save_path . $new_file_name); return ['path' => $save_path, 'url' => $save_url, 'name' => $file_name, 'new_name' => $new_file_name, 'ext' => $file_ext]; }