PHP code example of d3yii2 / d3files
1. Go to this page and download the library: Download d3yii2/d3files library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
d3yii2 / d3files example snippets
'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
],
],
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
'@d3yii2/d3files/migrations',
],
],
<?= d3yii2\d3files\widgets\D3FilesWidget::widget(
[
'model' => $model,
'model_id' => $model->id,
'title' => 'Widget Title',
'icon' => false,
'hideTitle' => false,
'readOnly' => false,
//'viewByFancyBox' => false,
//'controllerRoute'=>'/d3emails/email/', //use if different controllers
// 'actionColumn' => static function ($row) {
// return 'OK';
//}
]
)
<?= d3yii2\d3files\widgets\D3FilesPreviewWidget::widget(
[
'model' => $model,
'model_id' => $model->id,
'title' => 'Preview Widget Title',
'readOnly' => false,
]
)
if ($footer = $this->getPageFooter()):
'modules' => [
'd3files' => [
....
'disableController' => true, // set true to disable d3files controller to use model's controllers
.....
],
],
/**
* @inheritdoc
*/
public function behaviors()
{
$addBehaviors = [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['d3filesdownload', 'd3filesupload', 'd3filesdelete'],
'rules' => [
// deny all POST requests
[
'allow' => true,
'actions' => [
'd3filesdownload',
'd3filesopen',
'd3filesupload',
'd3filesdelete',
'd3fileseditnotes'
],
'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::class,
'modelName' => D3pPerson::class,
],
'd3filesupload' => [
'class' => d3yii2\d3files\components\UploadAction::class,
'modelName' => D3pPerson::class,
],
'd3filesdelete' => [
'class' => \d3yii2\d3files\components\DeleteAction::class,
'modelName' => D3pPerson::class,
],
// for D3FilesPreviewWidget
'd3filesopen' => [
'class' => Dd3yii2\d3files\components\ownloadAction::class,
'modelName' => D3pPerson::class,
'downloadType' => 'open'
],
// for D3FilesPreviewWidget
'd3filesopen' => [
'class' => 'd3yii2\d3files\components\DownloadAction',
'modelName' => RkInvoice::class,
'downloadType' => 'open'
],
// for D3FilesPreviewWidget
'd3fileseditnotes' => [
'class' => EditNotesAction::class,
'modelName' => D3pPerson::class,
],
];
}
d3yii2\d3files\widgets\D3FilesWidget::widget(
[
'model' => $model,
'model_id' => $model->id,
'title' => 'Attachments',
'icon' => false,
'hideTitle' => false,
'readOnly' => false
]
)
d3yii2\d3files\widgets\D3FilesPreviewWidget::widget([
'model' => $model, // Model to load attachment(s) from
'fileList' => [...] // Resulting array of the ModelD3Files::fileListForWidget if the attachments are joined already. Required if model not specified.
'defaultExtension' => 'pdf', // Optional (PDF by default),
'viewExtensions' => ['pdf'] // Optional.- (['pdf', 'png', 'jpg', 'jpeg'] by default)
])
x-frame-options: deny
X-Frame-Options:SAMEORIGIN
X-Frame-Options: sameorigin
X-Frame-Options: "allow-from https://example.com/"
public $uploadFile;
public function rules() {
return [
......,
[
['uploadFile'],
'file',
'skipOnEmpty' => true,
'extensions' => 'png, jpg, pdf, xls, doc'
],
];
}
use d3yii2\d3files\models\D3files;
$model->uploadFile = UploadedFile::getInstance($model, 'uploadFile');
D3files::saveYii2UploadFile($model->uploadFile, ModelName::className(), $model->id);
$form = ActiveForm::begin([
'id' => 'xxxxxxx',
'layout' => 'horizontal',
'enableClientValidation' => true,
'options' => [
'enctype' => 'multipart/form-data',
],
]
);
echo $form->field($model, 'uploadFile')->fileInput();
//$id is D3filesModel model's ID
$share = D3filesModel::createSharedModel($id, $expireDays, $leftLoadings);
$shared_id = $share['id'];
$shared_hash = $share['hash'];
$url = 'http://www.yoursite.com/index.php?r=d3files/d3files/downloadshare&id=' . $shared_id . '&hash=' . $shared_hash;
echo $url;
use d3yii2\d3files\models\D3files;
$filesList = D3files::getRecordFilesList($model::className(),$model->id)
public static function getRecordFiles(string $className, int $recordId): array
{
$files = [];
foreach (D3Files::getModelFilesList($className, $recordId, true) as $file) {
$fileHandler = new FileHandler([
'model_name' => $file['className'],
'model_id' => $file['id'],
'file_name' => $file['file_name']
]);
$files[] = [
'fileName' => $file['file_name'],
'filePath' => $fileHandler->getFilePath(),
];
}
return $files;
}
$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);
$model = Users::findOne($id);
D3filesModel::createCopy($fileModelId, Users::class, $model->id);