Untuk mengupload multiple files dengan fileinput widget cukup memodifikasi sedikit dari Tips sebelumnya.
Yang perlu dimodifikasi adalah pada _form.php,
1 |
<?= $form->field($model, 'namafile') |
menjadi array :
1 |
<?= $form->field($model, 'namafile[]') |
kemudian tambahkan berapa file yang bisa diupload dalam suatu waktu :
1 |
'maxFileCount'=> 10, |
Dan aktifkan multiple upload :
1 |
'options' => ['multiple'=>true, 'accept' => ''] |
Sehingga _form.php menjadi :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?= $form->field($model, 'namafile[]')->widget(FileInput::className(),[ 'options' => ['multiple'=>true, 'accept' => ''] 'pluginOptions' => [ 'showRemove'=> false, 'showUpload' => false, 'showCancel' => false, 'overwriteInitial' => false, 'initialPreviewConfig' => $json, 'previewFileType' => 'image', 'initialPreview' => $img, 'uploadAsync'=> true, 'maxFileSize' => 3*1024*1024, 'deleteUrl' => Url::to(['/file/delete-upload']), 'allowedExtensions' => ['jpg','png','jpeg'], 'maxFileCount'=> 10, ] ])?> <?php else : ?> <?= $form->field($model, 'namafile[]')->widget(FileInput::classname(), [ 'options' => ['multiple'=>true, 'accept' => ''] 'pluginOptions' => [ 'showUpload' => false, 'maxFileCount'=> 10, ] ]); ?> |
Ubah actionCreate pada Controller, dari getInstance menjadi getInstances dan tambahkan foreach sehingga menjadi seperti ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public function actionCreate() { $model = new File(); if ($model->load(Yii::$app->request->post())) { $model->namafile = UploadedFile::getInstances($model,'namafile'); foreach($model->namafile as $saving){ if($saving){ $file = $saving->name; if ($saving->saveAs(Yii::getAlias('@frontend').'/web/file/'.$file)){ $saving = $file; } } $simpan = new File(); $simpan->namafile = $file; $simpan->save(); } return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } |
Sehingga FileController.php menjadi seperti ini :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
<?php namespace frontend\controllers; use Yii; use common\models\File; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\web\UploadedFile; /** * FileController implements the CRUD actions for File model. */ class FileController extends Controller { public function actionIndex(){ $files = File::find()->all(); return $this->render('index',[ 'files' => $files, ]); } public function actionCreate() { $model = new File(); if ($model->load(Yii::$app->request->post())) { $model->namafile = UploadedFile::getInstances($model,'namafile'); foreach($model->namafile as $saving){ if($saving){ $file = $saving->name; if ($saving->saveAs(Yii::getAlias('@frontend').'/web/file/'.$file)){ $saving = $file; } } $simpan = new File(); $simpan->namafile = $file; $simpan->save(); } return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } public function actionUpdate($id) { $model = $this->findModel($id); $old_file = $model->namafile; if ($model->load(Yii::$app->request->post())) { $model->namafile = UploadedFile::getInstance($model,'namafile'); if($model->namafile){ $file = $model->namafile->name; if ($model->namafile->saveAs(Yii::getAlias('@frontend').'/web/file/'.$file)){ $model->namafile = $file; } } if (empty($model->namafile)){ $model->namafile = $old_file; } $model->save(); return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $model, ]); } } public function actionDelete($id) { $model = $this->findModel($id); @unlink(Yii::getAlias('@frontend') . '/web/file/' . $model->namafile); $model->delete(); return $this->redirect(['index']); } public function actionDeleteUpload() { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $keys = Yii::$app->request->post('key'); $key = explode(' ', $keys); $model = File::find()->where([ 'id' => $key[1], //'create_id' => Yii::$app->user->id ])->one(); if ($key[0] == 'namafile') { @unlink(Yii::getAlias('@frontend') . '/web/file/' . $model->namafile); $model->namafile = NULL; $model->save(false); } return []; } protected function findModel($id) { if (($model = File::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } } ?> |
Sekarang sudah bisa mengupload file lebih dari satu :
Function updatenya nggak bekerja saat mau nambah file yg akan di upload serta menghapus dan mengganti file yg akan di upload