Downloadd3files (inspired from d2files)
![License](https://poser.pugx.org/d3yii2/d3files/license)
Extension for file uploading and attaching to the models
Features
-
attach files to model record (it is possible to attach to one model multiple files)
-
widget for model view
-
access rights realised as standalone actions (separate download, upload, delete) by integrating in model's controllers
-
shared files for public access
Installation
php composer.phar require d3yii2/d3files dev-master
* add to config/web.php 'modules' => [
'd3files' => [
'class' => 'd3yii2\d3files\D3Files',
'uploadDir' => dirname(__DIR__) . '\upload\d3files',
'disableController' => false, // set true to disable d3files controller to use model's controllers
'hashSalt' => false, // Set salt in your web-local.php config, empty value will disable sharing
'sharedExpireDays' => 5,
'sharedLeftLoadings' => 5,
'controllerRoute' => 'delivery/attachments', //define controler route, where defined
],
],
-
migration configuration. Add to console migration definition path
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
'@d3yii2/d3files/migrations',
],
],
-
do migration
yii migrate
Usage
Widget
Allow upload, download, delete files for model record.
<?= d3yii2\d3files\widgets\D3FilesWidget::widget(
[
'model' => $model,
'model_id' => $model->id,
'title' => 'Widget Title',
'icon' => false,
'hideTitle' => false,
'readOnly' => false
]
) ?>
Access control
In config disableController set true for disabling use d3files controller, where no realised any access control. 'modules' => [
'd3files' => [
....
'disableController' => true, // set true to disable d3files controller to use model's controllers
.....
],
],
For implementing access control add separate actions for upload, download and delete to model controller. Can implement any standard Yii2 access control. For example RBAC.
/
* @inheritdoc
*/
public function behaviors()
{
$addBehaviors = [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['d3filesdownload', 'd3filesupload', 'd3filesdelete'],
'rules' => [
// deny all POST requests
[
'allow' => true,
'actions' => [
'd3filesdownload',
'd3filesupload',
'd3filesdelete',
],
'roles' => ['role1','role2'],
],
],
],
'verbs' => [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
'd3filedelete' => ['POST'],
'd3fileupload' => ['POST'],
],
],
];
return array_merge(parent::behaviors(), $addBehaviors);
}
public function actions() {
return [
'd3filesdownload' => [
'class' => 'd3yii2\d3files\components\DownloadAction',
'modelName' => RkInvoice::className(),
],
'd3filesupload' => [
'class' => 'd3yii2\d3files\components\UploadAction',
'modelName' => RkInvoice::className(),
],
'd3filesdelete' => [
'class' => 'd3yii2\d3files\components\DeleteAction',
'modelName' => RkInvoice::className(),
],
];
}
Widget d3yii2\d3files\widgets\D3FilesWidget::widget(
[
'model' => $model,
'model_id' => $model->id,
'title' => 'Attachments',
'icon' => false,
'hideTitle' => false,
'readOnly' => false
]
)
Active Form
-
to Active form model add property for uploading file
public $uploadFile;
-
to Active model for new attribute add rule
public function rules() {
return [
......,
[
['uploadFile'],
'file',
'skipOnEmpty' => true,
'extensions' => 'png, jpg, pdf, xls, doc'
],
];
}
use d3yii2\d3files\models\D3files;
-
in controller action after successful save() add
$model->uploadFile = UploadedFile::getInstance($model, 'uploadFile');
D3files::saveYii2UploadFile($model->uploadFile, ModelName::className(), $model->id);
-
in form to Active form set 'enctype' = 'multipart/form-data',
$form = ActiveForm::begin([
'id' => 'xxxxxxx',
'layout' => 'horizontal',
'enableClientValidation' => true,
'options' => [
'enctype' => 'multipart/form-data',
],
]
);
-
in form view add upload field
echo $form->field($model, 'uploadFile')->fileInput();
Shared (public) access
-
to create share implement share generation request in your code:
//$id is D3filesModel model's ID
$share = D3filesModel::createSharedModel($id, $expireDays, $leftLoadings);
$shared_id = $share['id'];
$shared_hash = $share['hash'];
Get record files list
use d3yii2\d3files\models\D3files;
$filesList = D3files::getRecordFilesList($model::className(),$model->id)
Attach existing file to record
$fileTypes = '/(gif|pdf|dat|jpe?g|png|doc|docx|xls|xlsx|htm|txt|log|mxl|xml|zip)$/i';
$model = Users::findOne($id);
$fileName = 'MyAvatar.jpg';
$filePath = '/temp/avatar.jpg';
D3files::saveFile($fileName, Users::className(), $model->id, $filePath, $fileTypes);
Change log
- 0.9.0 (Feb 26, 2017) - added RU translation
- 0.9.3 (May 29, 2017) - auto creating upload directories
- 0.9.4 (Nov 16, 2017) - added parameter controllerRoute
|