1
votes

I'm trying to add Dropzone Extension to my application in Yii, which allows asynchronous file uploading. http://www.yiiframework.com/extension/yii-dropzone/

The first thing i did was putting the downloaded folder called "dropzone" into my extensions folder "C:\xampp\htdocs\site\protected\extensions".

And here is my code for the action in the controller (MainController.php)

public function actionUpload()
{
    $test = rand(100000, 999999); //TEST
    var_dump($test);

    $model = new UploadFile;
    
    if(isset($_FILES['images'])){
        
        $model->images = CUploadedFile::getInstancesByName('images');
        
        $path = Yii::getPathOfAlias('webroot').'/uploads/';
        
        //Save the images
        foreach($model->images as $image)
        {
            $image->saveAs($path);
        }                    
    }
    
    $this->render('upload', array('model' => $model));
}

the view (upload.php)

<?php
$this->widget('ext.dropzone.EDropzone', array(
    'model' => $model,
    'attribute' => 'images',
    'url' => $this->createUrl('file/upload'),
    'mimeTypes' => array('image/jpeg', 'image/png'),
    'options' => array(),
));
?>

and the model (UploadFile.php)

<?php

class UploadFile extends CFormModel
{
    public $images;
    
    public function rules(){
        return array
        (
            array(
                "images",
                'file',
                'types' => 'jpg,gif,png',
            ),
        );
    }
}

When I run it I can see the Dropzone interface and I can add images dragging them or selection them from the file explorer. It appears their respective progress bar and a success mark, but nothing appear in the directory of uploads, and any error is shown neither in the IDE (Netbeans) nor in the Chrome console.

I did some print tests and I realize that the code inside the 'actionUpload' is being executed only the first time (when it draws the view), but when its called from the dropzone widget it do nothing.

I'd really appreciate if you have a solution for this. I'd love if someone could give me a simple working example of this extension. Thanks.

1
You are running through the CURRENT set of images for the model ($model->images) which will be empty the first time around (and always) because you never set any values for it. Rather, run through $_FILES['images'] - crafter
You are right, I missed that. I already edited the controller. Anyway it is not working yet, I also tried printing a test random number as you can see now in the code. It is being printed only 1 time (when the view is rendered). So I think the problem is with the widget that isn't calling the action. - Diego Mellizo

1 Answers

1
votes

As I understand, dropzone uploads files one by one, not all together. So $model->images holds only one image object. And foreach cycle fails.