Upload File pakai Jappendo, tapi untuk variable $_FILES ada error Undefined Offset 2. Berikut codingnya
_form
<div class="form"> <?php $form = $this->beginWidget('bootstrap.widgets.BootActiveForm', array( 'id' => 'horizontalForm', 'type' => 'horizontal', 'htmlOptions' => array('enctype' => 'multipart/form-data'), )); ?> <div class="controls"> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> </div> <table style="width: 80%; margin-left: -30px;"> <tr> <td> <?php echo $form->dropDownListRow($model, 'genre_id', CHtml::listData(Genre::model()->findAll(array('order' => 'genre_name ASC')), 'id', 'genre_name')); ?> </td> <td> <?php $this->widget('bootstrap.widgets.BootButton', array( 'label' => '+ Genre', 'url' => '#createGenre', 'type' => 'primary', 'htmlOptions' => array('data-toggle' => 'modal'), )); ?> </td> </tr> <tr> <td> <?php echo $form->dropDownListRow($model, 'artist_id', CHtml::listData(Artist::model()->findAll(array('order' => 'artist_name ASC')), 'id', 'artist_name')); ?> </td> <td> <?php $this->widget('bootstrap.widgets.BootButton', array( 'label' => '+ Artist', 'url' => '#createArtist', 'type' => 'primary', 'htmlOptions' => array('data-toggle' => 'modal'), )); ?> </td> </tr> <tr> <td><?php echo $form->textFieldRow($model, 'album'); ?></td> <td><?php echo $form->textFieldRow($model, 'year_release'); ?></td> </tr> <tr> <td colspan="2"><?php echo $form->fileFieldRow($model, 'cover_url'); ?></td> </tr> <tr> <td><?php echo $form->textFieldRow($model, 'producer'); ?></td> <td><?php echo $form->textFieldRow($model, 'author'); ?></td> </tr> <tr> <td colspan="2"><?php // echo $form->textFieldRow($model, 'song_title'); ?></td> </tr> <tr> <td colspan="2"> <?php // echo $form->fileFieldRow($model, 'title', array('hint' => 'MP3 Properties : 128bit rate, Frequency 44100Hz, Stereo')); ?> <div style="margin-left: 10%;"> <?php $this->widget('application.extensions.jappendo.JAppendo', array( 'id' => 'songUpload', 'model' => $model, 'viewName' => 'songUpload', 'labelDel' => 'Remove Row', 'cssFile' => 'css/jquery.appendo.css' )); ?> </div> </td> </tr> </table> <div class="form-actions"> <?php $this->widget('bootstrap.widgets.BootButton', array('buttonType' => 'submit', 'type' => 'primary', 'icon' => 'ok white', 'label' => $model->isNewRecord ? 'Create' : 'Save')); ?> <?php // echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form --> <!-- Modal Dialog Genre --> <?php $this->beginWidget('bootstrap.widgets.BootModal', array('id' => 'createGenre')); ?> <div class="modal-header"> <a class="close" data-dismiss="modal">×</a> <h3>Create Genre</h3> </div> <div class="modal-body"> <form id="genre-form" class="form-horizontal" method="post" action="<?php echo $this->createUrl('/genre/Createfromsong') ?>"> <label class="control-label required" for="Genre_genre_name"> Genre Name <span class="required">*</span> </label> <input id="Genre_genre_name" type="text" name="Genre[genre_name]" maxlength="100" size="60" /> <input type="submit" value="Create" /> </form> </div> <?php $this->endWidget(); ?> <!-- Modal Dialog Artist --> <?php $this->beginWidget('bootstrap.widgets.BootModal', array('id' => 'createArtist')); ?> <div class="modal-header"> <a class="close" data-dismiss="modal">×</a> <h3>Create Artist</h3> </div> <div class="modal-body"> <form class="form-horizontal" action="<?php echo Yii::app()->createUrl('/artist/Createfromsong') ?>" method="post"> <label class="control-label required" for="Artist_artist_name"> Artist Name <span class="required">*</span> </label> <input id="Artist_artist_name" type="text" name="Artist[artist_name]" maxlength="100" size="60" /> <input type="submit" value="Create" /> </form> </div> <?php $this->endWidget(); ?>
songUpload view untuk Jappendo
<table class="appendo-gii" cellpadding="5px;" id="<?php echo $id ?>" > <caption> MP3 Properties : 128bit rate, Frequency 44100Hz, Stereo </caption> <thead> <tr> <th>Song Title</th> <th>Song File</th> </tr> </thead> <tbody> <?php if ($model->song_title == null): ?> <tr> <td><?php echo CHtml::textField('song_title[]', '', array('style' => 'width:200px')); ?></td> <td><?php echo CHtml::fileField('title[]', ''); ?></td> </tr> <?php else: ?> <?php for ($i = 0; $i < sizeof($model->title); $i++): ?> <tr> <td><?php echo CHtml::textField('song_title[]', $model->enum_name[$i], array('style' => 'width:200px')); ?></td> <td><?php echo CHtml::fileField('title[]', $model->enum_name[$i]); ?></td> </tr> <?php endfor; ?> <tr> <td><?php echo CHtml::textField('song_title[]', '', array('style' => 'width:200px')); ?></td> <td><?php echo CHtml::fileField('title[]', ''); ?></td> </tr> <?php endif; ?> </tbody> </table>
Controllernya
public function actionCreatebatch() { $renderForm = new SongStorage; if (isset($_FILES['title'])) { $count = count($_FILES['title']); for ($i = 0; $i <= $count; $i++) { $model = new SongStorage; // $model->genre_id = $_POST['genre_id']; // $model->artist_id = $_POST['artist_id']; // $model->year_release = $_POST['year_release']; $model->view_count = 0; // $model->album = $_POST['album']; // $model->producer = $_POST['producer']; // $model->author = $_POST['author']; $cover_url = CUploadedFile::getInstance($model, 'cover_url'); $model->cover_url = $cover_url->getName(); //$songFile = CUploadedFile::getInstancesByName($_FILES['title'][$i]); $model->title = $_FILES['title']['name'][$i]; $model->file_type = $_FILES['title']['type'][$i]; $model->file_size = $_FILES['title']['size'][$i]; $model->song_title = $_POST['song_title'][$i]; if ($model->save()) { $cover_url->saveAs(Yii::app()->basePath . '/../images/album/' . $model->cover_url); move_uploaded_file($_FILES['title']['tmp_name'][$i], Yii::app()->basePath . '/../songsstorage/' . $_FILES['title']['name'][$i]); } } $this->redirect(array('viewalbum', 'album' => $model->album)); } $this->render('create', array( 'model' => $renderForm, )); }
terima kasih sebelumnya