卡拉库姆 / yii2-path-registry
此扩展提供了一个基于最大占用目录的文件存储目录生成器。
0.4.0
2016-08-25 20:30 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-12 02:07:36 UTC
README
此扩展提供了一个基于最大占用目录的文件存储目录生成器。
每次调用getPath
方法时,它会搜索最空的目录。当所有目录根据每个目录的最大文件数(默认为1000个文件)已满时,扩展将生成新的目录部分(默认20个目录)。使用带有每个文件的目录必须通过方法countUpPath
进行标记。当文件从目录中删除时,必须调用方法countDownPath
。
可以针对不同的命名空间使用不同的填充设置。
安装
安装此扩展的首选方法是通过composer。
运行
php composer.phar require --prefer-dist karakum/yii2-path-registry "*"
或添加
"karakum/yii2-path-registry": "*"
到您的composer.json
文件的require部分。
配置应用程序组件
'components' => [
...
'pathManager' => [
'class' => 'karakum\PathRegistry\PathManager',
'defaultMaxFiles' => 1000,
'defaultNewFolders' => 20,
'namespaces' => [
'avatar' => [
'path' => '@webroot/uploads/avatar',
'url' => '@web/uploads/avatar', // Direct download link
'maxFiles' => 100,
'newFolders' => 5
],
'files' => [
'path' => '@app/secure',
'url' => ['file/download'], // Through controller
],
],
],
'urlManager' => [
...
'rules' => [
'download/<id:\d+>/<name>' =>'file/download',
...
],
...
],
...
],
创建迁移
$ yii migrate/create path_organizer
并修改如下
<?php use yii\db\Migration; require(Yii::getAlias('@karakum/PathRegistry/migrations/m160606_163120_path_organizer_init.php')); class mXXXXXX_XXXXXX_path_organizer extends Migration { public function up() { $path = new m160606_163120_path_organizer_init(); $path->up(); } public function down() { $path = new m160606_163120_path_organizer_init(); $path->down(); } }
迁移将创建具有默认名称{{%path_organizer}}
的表。要更改它,请使用PathManager组件的pathTable
属性。
使用方法
一旦安装了此扩展,只需在您的代码中通过
<?= $avatarPath = Yii::$app->pathManager->getPath('avatar'); $path = $avatarPath->getPath(); // '7b08aea20ff07411b74b97ebe7fe6bf8' $directory = $avatarPath->getFullPath(); // '/var/www/yii2-app/web/uploads/avatar/7b08aea20ff07411b74b97ebe7fe6bf8' $filename = $avatarPath->getFullPath('photo.png'); // '/var/www/yii2-app/web/uploads/avatar/7b08aea20ff07411b74b97ebe7fe6bf8/photo.png' $file = $avatarPath->getPath('photo.png'); // '7b08aea20ff07411b74b97ebe7fe6bf8/photo.png' $imgSrc = $avatarPath->getUrl($file, [ // 'http://mysite.com/uploads/avatar/7b08aea20ff07411b74b97ebe7fe6bf8/photo.png' 'id' => 1, 'name' => 'Photo_name.png' ]); $filesPath = Yii::$app->pathManager->getPath('files'); $path = $filesPath->getPath(); // '06524c83dc331625baeece8a8b4b5022' $directory = $filesPath->getFullPath(); // '/var/www/yii2-app/secure/06524c83dc331625baeece8a8b4b5022' $filename = $filesPath->getFullPath('secret.txt'); // '/var/www/yii2-app/secure/06524c83dc331625baeece8a8b4b5022/secret.txt' $file = $filesPath->getPath('secret.txt'); // '06524c83dc331625baeece8a8b4b5022/secret.txt' $fileUrl = $filesPath->getUrl($file, [ // 'http://mysite.com/download/2/Secret_data.txt' 'id' => 2, 'name' => 'Secret_data.txt' ]); ?>