Working with Yii framework 2.0 I want to be able to upload multiple files. Following Yii 2 documentation, under subsection Upload Multiple Files
I have the following model.
class Newsletter extends \yii\db\ActiveRecord {
public $attachment_file;
public function rules()
{
return [
[['attachment_file'], 'file', 'maxFiles' => 5],
];
}
public function upload() {
if ($this->validate()) {
foreach ($this->attachment_file as $file) {
echo '<pre>'; print_r($file); echo '</pre>';
}
return true;
} else {
return false;
}
}
}
Below is my view.
<?php use yii\widgets\ActiveForm; ?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'attachment_file[]')->fileInput(['multiple' => true,]) ?>
<button>Submit</button>
<?php ActiveForm::end() ?>
In my controller I have the following code snippet.
if (Yii::$app->request->isPost) {
$model->attachment_file = UploadedFile::getInstances($model, 'attachment_file');
if ($model->upload()) {
die();
// file is uploaded successfully
return;
}
}
With all the code above I expect I can select multiple files with one input file element. But it is not like what I expect. When I select multiple files with one same input file element and hit Submit I saw only the last selected file. So I start to have doubt about what I am doing. Did I do anything wrong? Or do I need to add input file element several times, one input file element for one uploading file?
Uploading files in Yii is usually done with the help of yii\web\UploadedFile which encapsulates each uploaded file as an UploadedFile object. Combined with yii\widgets\ActiveForm and models, you can easily implement a secure file uploading mechanism.
If you know all that, then you can start in a week. After a month or two you'll be doing things correctly. After about 3 months you should be comfortable. After about a year you may master the framework if you want that.
Controllers are part of the MVC architecture. They are objects of classes extending from yii\base\Controller and are responsible for processing requests and generating responses.
Each Yii application system contains a single application object which is created in the entry script and is globally accessible through the expression \Yii::$app . Info: Depending on the context, when we say "an application", it can mean either an application object or an application system.
See what I tried: view code
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($uploadForm, 'files[]')->fileInput(['multiple' => true]) ?>
<button class="btn btn-primary">Upload</button>
<?php ActiveForm::end() ?>
in controller
use yii\web\UploadedFile;
use app\models\MultipleUploadForm;
use app\models\ProductImage;
.......
function actionUploadImage() {
$form = new MultipleUploadForm();
if (Yii::$app->request->isPost) {
$form->files = UploadedFile::getInstances($form, 'files');
if ($form->files && $form->validate()) {
foreach ($form->files as $file) {
$image = new ProductImage();
if ($image->save()) {
$file->saveAs($image->getPath());
}
}
}
}
return $this->render('uploadImage', [
'uploadForm' => $form,
]);
}
MultipleUploadForm model
use yii\base\Model;
use yii\web\UploadedFile;
class MultipleUploadForm extends Model
{
/**
* @var UploadedFile[] files uploaded
*/
public $files;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['files'], 'file', 'extensions' => 'jpg', 'mimeTypes' => 'image/jpeg', 'maxFiles' => 10, 'skipOnEmpty' => false],
];
}
}
This code is working for me. Hope this works for you too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With