Fork me on GitHub

EFileUploadAction

File upload action.

Usage

// add action to controller
public function actions()
{
    return array(
        // ...
        'upload'=>array(
            'class'=>'ext.yiiext.actions.fileUpload.EFileUploadAction',
            // The data model which contains file attribute with validation rules.
            'model'=>null,
            // The model attribute.
            'attribute'=>null,
            // The input field name. This must be resolve from model and attribute.
            'name'=>'upload-file',
            // Try create directory if not exists. Defaults to false.
            'createDirectory'=>false,
            // Which means the widest possible access.
            'createDirectoryMode'=>0644,
            // Which create directories recursive.
            'createDirectoryRecursive'=>false,
            // The rule for generate filename.
            // i.e. 'filenameRule'=>'md5($file->name).".".$file->extensionName',
            'filenameRule'=>null,
            // The filename. If not set will be copied original filename.
            'filename'=>null,
            // The directory where save files.
            'path'=>null,
            'onBeforeUpload'=>function($event)
            {
                // Change default path via event.
                $event->sender->path=Yii::getPathOfAlias('webroot').'/files';
            },
            'onBeforeSave'=>function($event)
            {
                // Add error and cancel uploading.
                $event->sender->addError(Yii::t('yiiext','Process stopped!'));
                $event->isValid=false;
            },
            'onAfterSave'=>function($event)
            {
                // i.e. make thumb for uploaded image.
            }
            'onAfterUpload'=>function($event)
            {
                if($event->sender->hasErrors())
                    // Show error if exists.
                    echo implode(', ',$event->sender->getErrors());
                else
                    // Return url.
                    echo str_replace(Yii::getPathOfAlias('webroot'),'',$event->sender->path).'/'.$event->sender->filename;
 
                // Stop application.
                exit;
            }
        ),
        // ...
    );
}

Events

Action has 4 events and runs in the following order: onBeforeUpload, onBeforeSave, onAfterSave, onAfterUpload. * OnBeforeUpload - this event is mainly intended to change the default settings for the action: save path, file name, etc. * OnBeforeSave - intended for a more detailed validation of the downloaded file, what can be done by means of the model. This event can be canceled to save the file by setting the $ event->isValid=false * OnAfterSave - an event designed to manipulate already saved file. * OnAfterUpload - in this event are invited to check the load, save or no. And show the error.

Changelog

0.3 [+] Add getErrors(), hasErrors(), addError() method. (Veaceslav Medvedev) [+] Add onBeforeUpload and onAfterUpload event. Details in readme.txt (Veaceslav Medvedev) [*] Event onBeforeSave can cancel process and file do not be save. (Veaceslav Medvedev) [-] Remove setter and getter to path and filename. (Veaceslav Medvedev)

0.2

Warning: this version is not backwards compatible to 0.1.

[*] Add setter and getter to path and filename. Now you can change it from beforeSave event. (Veaceslav Medvedev) [-] Delete property exitOnAjax. You can stop appliaction via event. (Veaceslav Medvedev)

0.1

[+] Initial public release (Veaceslav Medvedev)

Action for file uploads handling.

Documentation

Downloads (Tags)

Resources