I would like to upload multiple files at once. I have
a model:
class KakiKingModel extends ActiveRecord {
public $uploadedFiles;
public function rules() {
return [
[['uploadedFiles'], 'file', 'extensions' => 'txt', 'checkExtensionByMimeType' => false, 'skipOnEmpty' => true, 'maxFiles' => 2]];
}
...
controller:
use yii\web\UploadedFile;
...
public function actionUpload() {
$model = new KakiKingModel;
$t = new KakiKingModel;
if (Yii::$app->request->isPost) {
$files = UploadedFile::getInstances($model, 'uploadedFiles');
$t = [];
$i = 0;
foreach ($files as $i => $file) {
$t[$i] = new KakiKingModel;
$t[$i]->contentUploadedFile = file($file->tempName);
$t[$i]->assign(); // assign file content to model attributes
$i++;
}
if (Model::validateMultiple($t)) {
foreach ($t as $item) {
$item->save(false);
}
return $this->redirect(['index']);
} else {
return $this->render('upload', [
'model' => $model,
't' => $t,
]);
}
}
return $this->render('upload', [
'model' => $model,
't' => $t,
]);
}
view:
$form = ActiveForm::begin([
....
'options' => ['enctype' => 'multipart/form-data'],
...
<?= $form->field($model, 'uploadedFiles[]')->fileInput(['multiple' => true]) ?>
and the problem is, it accepts any other type of files also! Why is that? What am I doing wrong? Thanks! UPDATE: I have changed my content a bit, so that you can better understand why I find it disturbing that it doesn't work. It should work IMHO. Can you please help me? Thank you!