Home >Backend Development >PHP Tutorial >Yii2 implements form upload file function
This article mainly introduces the example code of Yii2 using forms to upload files. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
1. Single file upload
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'], ]; } }
Then create a view file with the following content
<?php use 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 the controller file, the content is as follows
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 did not use model->load(...) here, but used 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 passes, the uploaded file is saved in the uploads folder, that is, uploads in the web directory.
Some optional configuration options
The uploaded file cannot be empty
public function rules() { return [ [['file'], 'file', 'skipOnEmpty' => false], ]; }
The upload type can be checked not only based on the extension, but also based on the content of the file Check
public function rules() { return [ [['file'], 'file', 'extensions' => 'jpg, png', 'mimeTypes' => 'image/jpeg, image/png',], ]; }
2. Multiple file upload
If you want to upload multiple files at one time, you only need to adjust a few 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:
<?php use yii\widgets\ActiveForm; $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> <?= $form->field($model, 'file[]')->fileInput(['multiple' => true]) ?> <button>Submit</button> <?php ActiveForm::end(); ?>
The difference from single file upload is the following sentence
$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]); } }
This way, multiple files can be uploaded.
Related recommendations:
PHP and AjaxForm implement asynchronous file upload with progress bar
jQuery ajaxupload plug-in implementation Detailed explanation of uploading files without refreshing
Detailed explanation of Ajax form asynchronous upload file example code
The above is the detailed content of Yii2 implements form upload file function. For more information, please follow other related articles on the PHP Chinese website!