An example of Yii2 using forms to upload files

巴扎黑
Release: 2023-03-15 08:42:01
Original
1588 people have browsed it

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 and take a look.

yii2 It is often used to upload files through forms. How to upload them?

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'],
    ];
  }
}
Copy after login

Create another view file with the following content


<?php
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin([&#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;]]) ?>

<?= $form->field($model, &#39;file&#39;)->fileInput() ?>

<button>Submit</button>

<?php ActiveForm::end() ?>
Copy after login

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, &#39;file&#39;);

      if ($model->file && $model->validate()) {        
        $model->file->saveAs(&#39;uploads/&#39; . $model->file->baseName . &#39;.&#39; . $model->file->extension);
      }
    }

    return $this->render(&#39;upload&#39;, [&#39;model&#39; => $model]);
  }
}
Copy after login

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 [
    [[&#39;file&#39;], &#39;file&#39;, &#39;skipOnEmpty&#39; => false],
  ];
}
Copy after login

The upload type can not only be based on the extension Check, you can also check based on the content of the file


public function rules()
{
  return [
    [[&#39;file&#39;], &#39;file&#39;, &#39;extensions&#39; => &#39;jpg, png&#39;, &#39;mimeTypes&#39; => &#39;image/jpeg, image/png&#39;,],
  ];
}
Copy after login

2. Multiple file upload

If 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 [
      [[&#39;file&#39;], &#39;file&#39;, &#39;maxFiles&#39; => 10], // <--- here!
    ];
  }
}
Copy after login

View:


<?php
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([&#39;options&#39; => [&#39;enctype&#39; => &#39;multipart/form-data&#39;]]);
?>

<?= $form->field($model, &#39;file[]&#39;)->fileInput([&#39;multiple&#39; => true]) ?>

  <button>Submit</button>

<?php ActiveForm::end(); ?>
Copy after login

The difference from single file upload is the following sentence


$form->field($model, &#39;file[]&#39;)->fileInput([&#39;multiple&#39; => true])
Copy after login

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, &#39;file&#39;);

      if ($model->file && $model->validate()) {
        foreach ($model->file as $file) {
          $file->saveAs(&#39;uploads/&#39; . $file->baseName . &#39;.&#39; . $file->extension);
        }
      }
    }

    return $this->render(&#39;upload&#39;, [&#39;model&#39; => $model]);
  }
}
Copy after login

In this way, multiple files can be uploaded.

The above is the detailed content of An example of Yii2 using forms to upload files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template