Yii2 uses the example code of form Upload File, yii2 form
Yii2 is often used to upload files using forms. How can I upload files?
1. Upload a single file
First, create a model models/UploadForm. php with the following content:
namespace app\models;use yii\base\Model;use yii\web\UploadedFile;/** * UploadForm is the model behind the upload form. */class UploadForm extends Model{ /** * @var UploadedFile file attribute */ public $file; /** * @return array the validation rules. */ public function rules() { return [ [['file'], 'file'], ]; }}
Create a view file with the following content:
<?phpuse yii\widgets\ActiveForm;?><?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?><?= $form->field($model, 'file')->fileInput() ?><button>Submit</button><?php ActiveForm::end() ?>
Finally, create a controller file with the following content:
namespace app\controllers;use Yii;use yii\web\Controller;use app\models\UploadForm;use yii\web\UploadedFile;class SiteController extends Controller{ public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->file && $model->validate()) { $model->file->saveAs('uploads/' . $model->file->baseName . '.' . $model->file->extension); } } return $this->render('upload', ['model' => $model]); }}
Note that we do not use model-> load (...), but use UploadedFile: getInstance (...). The difference is that the latter will not execute $ model-> validate (), so you need to manually execute $ model-> validate () to check the validity of the data. If the verification succeeds, the uploaded file is saved in the uploads folder, that is, the uploads under the web directory.
Some optional configuration options
The file to be uploaded cannot be blank.
public function rules(){ return [ [['file'], 'file', 'skipOnEmpty' => false], ];}
The upload type, which can be verified based on the extension and the file content.
public function rules(){ return [ [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',], ];}
2. Multifile upload
If you want to upload multiple files at a time, you only need to adjust several parameters to achieve the goal.
Model:
class UploadForm extends Model{ /** * @var UploadedFile|Null file attribute */ public $file; /** * @return array the validation rules. */ public function rules() { return [ [['file'], 'file', 'maxFiles' => 10], // <--- here! ]; }}
View:
<?phpuse yii\widgets\ActiveForm;$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);?><?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?> <button>Submit</button><?php ActiveForm::end(); ?>
Which of the following statements is different from uploading a single file?
$form->field($model, 'file[]')->fileInput(['multiple' => true])
Controller:
namespace app\controllers;use Yii;use yii\web\Controller;use app\models\UploadForm;use yii\web\UploadedFile;class SiteController extends Controller{ public function actionUpload() { $model = new UploadForm(); if (Yii::$app->request->isPost) { $model->file = UploadedFile::getInstances($model, 'file'); if ($model->file && $model->validate()) { foreach ($model->file as $file) { $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension); } } } return $this->render('upload', ['model' => $model]); }}
In this way, you can upload multiple files.
Reference https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.