简体   繁体   中英

Data doesn't get saved into the database table when using Yii CMultiFileUpload

I am trying to implement multi files upload within my application. I was referring to this article and tried out this example , and it works (basically, they are the same). But when I try to implement it within my app for some reason it's not saving any of the data into the database table.

Please note that I also checked similar posts here and they weren't helpful.

So, when I upload files into the folder (that work's just fine) I need filename and sessionID to be saved into the table. Here is my table:

CREATE TABLE IF NOT EXISTS `photos` (
  `id` int(11) NOT NULL AUTO_INCREMENT, 
  `filename` varchar(45) NOT NULL,
  `amount` int(11) DEFAULT NULL,
  `pic_size_id` int(11) DEFAULT NULL,
  `users_sid` varchar(40) NOT NULL
  PRIMARY KEY (`id`), 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Here is my _form.php file:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'photos-form',
    'enableAjaxValidation'=>false,
    'htmlOptions' => array(
            'multiple' => 'multiple',
            'enctype' => 'multipart/form-data',            
            )
)); ?>

<?php echo $form->errorSummary($model); ?>

<?php
$this->widget('CMultiFileUpload', array(
'name'=> 'images', 
'accept' => 'jpeg|jpg|gif|png', 
'duplicate' => 'Duplicate file!', 
'denied'=> 'Invalid file type', 
));
?>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>  

<?php  $this->endWidget(); ?>

</div><!-- form -->

And here is my actionCreate() within PhotosController:

 public function actionCreate()
    {
            $model=new Photos;

            $images = array();
            $images = CUploadedFile::getInstancesByName('images');

                if (isset($images) && count($images) > 0) 
                {
                    // go through each uploaded image
                    foreach ($images as $image => $pic) 
                    {
                        if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$pic->name)) 
                        {                              
                            $img_add = new Photos();
                            $img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model
                            $img_add->users_sid = Yii::app()->session->sessionID; // this links your picture model to the main model (like your user, or profile model)
                            if(!$img_add->save()) // save your imagesDONE
                            {
                                var_dump($img_add->getErrors());
                                var_dump($pic->name);                                    
                            }                                
                        }
                    }                 
                }             

            $this->render('create',array(
                    'model'=>$model,
            ));
    }

var_dump($img_add->getErrors()) otuput is: array(1) { ["filename"]=> array(1) { [0]=> string(25) "Filename cannot be blank." } }

var_dump($pic->name) otuput is: string(37) "windows_metro-wallpaper-1920x1080.jpg", so $pic->name contains filename.

And I really don't know why it's not adding anything, when in the same example it works.

Also it's working when I have single upload, but with slightly different actionCreate() and _form.php of course:

PhotosController.php:

public function actionCreate()
    {
        $model=new Photos;
        if(isset($_POST['Photos']))
        {
            $model->attributes=$_POST['Photos'];
                        $model->filename=CUploadedFile::getInstance($model,'filename');
            if($model->validate())
                        {
                            $model->filename->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$model->filename);

                            $photo = new Photos;
                            $photo->filename = $model->filename;
                            $photo->users_sid = Yii::app()->session->sessionID;
                            if($photo->save())
                            {
                                $this->redirect(array('view','id'=>$model->id));
                            }
                        }           
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

_form.php :

 <div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'photos-form',
    'enableAjaxValidation'=>false,
    'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>

    <?php echo $form->errorSummary($model); ?>

    <!--<div class="row">
        <?php echo $form->labelEx($model,'filename'); ?>
        <?php echo $form->fileField($model,'filename'); ?>
        <?php echo $form->error($model,'filename'); ?>
    </div> -->

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>  

<?php  $this->endWidget(); ?>

</div><!-- form -->

a) check, if each $pic is not null. (see my code)

b) for testing, set filename-attribute as safe in photo-model-rules

if (isset($images) && count($images) > 0) 
{
    // go through each uploaded image
    foreach ($images as $image => $pic) 
    {
        if(!$pic || !$pic->getName()) {
            echo "pic is empty";
            continue;
        }

        if ($pic && $pic->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$pic->name)) 
                {

            // code goes here 

        }
    }       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM