vatandoost / filemanager
Yii2 文件管理器
Requires
- php: >=7.0.0
- nicolab/php-ftp-client: ^1.5
- yiisoft/yii2: ~2.0.0
- yiisoft/yii2-bootstrap4: ^2.0
This package is auto-updated.
Last update: 2024-09-12 13:17:18 UTC
README
此模块是保存和管理Yii2中文件的一种简单方法
特性
- 按两大类管理文件
- 私有
- 公共
- 多个处理器
- ftp 或 sftp
- 本地
- 管理类以管理任何文件类型
- 选择器小部件,用于从文件管理器中选择文件
- filetrait,用于模型类,以便轻松管理和保存文件
- 文件管理器 UI
- 预览文件
- Google Docs 预览器
- lightbox 图像预览器
- PDF 预览器
- jplayer 视频和音频预览
- 使用 unoconv 的本地办公室预览器
- 重命名文件
- 删除文件
- 搜索和排序文件
- 预览文件
- 从另一个 URL 上传文件
- 调整图像大小
- 使用 blueimp jquery 文件上传上传文件
- FileHelper 类用于管理应用程序中的文件
- 在应用程序中获取文件查询,以便通过选择文件地址与其他查询合并
- 使用 bootstrap4 主题
- 多语言
演示
屏幕截图
安装
使用 composer
composer require vatandoost/filemanager
应用迁移
yii migrate --migrationPath=vendor/vatandoost/filemanager/migrations
配置
在您的配置文件中,将文件管理器模块添加到模块中,并将文件管理器模块添加到您的引导组件中
'modules' => [
'filemanager' => [
'class' => 'vatandoost\filemanager\Module',
'publicFiles' => '@frontend/web/uploads',
'privateFiles' => '@frontend/uploads',
]
],
'bootstrap' => ['filemanager']
可用的配置
'filemanager' => [
'class' => 'vatandoost\filemanager\Module',
'title' => 'Yii2 filemanager',
'publicFiles' => '@frontend/web/uploads',
'privateFiles' => '@frontend/uploads',
'publicFilesUrl' => '/uploads',
'host' => 'http://optional.com:20180', // this param is option, if not exist it uses current server info
'params' => [
'upload_files' => true,
'url_upload' => true,
'default_handler' => FileType::HANDLER_TYPE_LOCAL,
'jplayer_exts' => array("mp4", "flv", "webmv", "webma", "webm", "m4a", "m4v", "ogv", "oga", "mp3", "midi", "mid", "ogg", "wav"),
'cad_exts' => array('dwg', 'dxf', 'hpgl', 'plt', 'spl', 'step', 'stp', 'iges', 'igs', 'sat', 'cgm', 'svg'),
// Preview with Google Documents
'googledoc_enabled' => true,
'googledoc_file_exts' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'odt', 'odp', 'ods'),
'image_extensions' => ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'ico'],
'file_extensions' => ['doc', 'docx', 'rtf', 'pdf', 'xls', 'xlsx', 'txt', 'csv', 'html', 'xhtml', 'psd', 'sql', 'log', 'fla', 'xml', 'ade', 'adp', 'mdb', 'accdb', 'ppt', 'pptx', 'odt', 'ots', 'ott', 'odb', 'odg', 'otp', 'otg', 'odf', 'ods', 'odp', 'css', 'ai', 'kmz', 'dwg', 'dxf', 'hpgl', 'plt', 'spl', 'step', 'stp', 'iges', 'igs', 'sat', 'cgm', 'tiff', ''], //Files
'video_extensions' => ['mov', 'mpeg', 'm4v', 'mp4', 'avi', 'mpg', 'wma', "flv", "webm"],
'audio_extensions' => ['mp3', 'mpga', 'm4a', 'ac3', 'aiff', 'mid', 'ogg', 'wav'],
'misc_extensions' => ['zip', 'rar', 'gz', 'tar', 'iso', 'dmg'],
//size of image boxes in filemanager ui
'image_preview_size' => [
'w' => 120,
'h' => 100,
],
'rename_files' => true,
'delete_files' => true,
// ftp handler options
'ftp' => [
'ftp_host' => "example.com", //put the FTP host
'ftp_user' => "example",
'ftp_pass' => "*****",
'ftp_public_folder' => "/public_html/filemanager",
'ftp_private_folder' => "/filemanager/uploads",
'ftp_base_url' => "https://example.com/filemanager",
'ftp_ssl' => false,
'ftp_port' => 21,
],
// convert office files to pdf format
'convert_pdf_preview' => false,
'unoconvBaseUrl' => 'localhost',
],
'superAdminRole' => 'filemanager_admin',
]
工作原理
有两个表来保存文件信息
- 表 file_type
- 表 files
表 file_type:它维护有关类型为 . 的文件的信息。要使用文件管理器,您必须首先创建一个 file_type 行。
如果您未定义它,则在模型中保存文件时将创建它。
每种文件类型都有一个管理类来管理它们的文件( Yii2 模型类)
fileType 字段
'file_type_id' => 'File Type ID', // integer autoincrement 'name' => 'Name', // unique name 'title' => 'Title', // title to show in filemanager ui 'is_public' => 'Is Public', // (boolean) show files are public or private 'mime_types' => 'Mime Types', 'max_size' => 'Max Size', // maximum file size to upload 'extensions' => 'Extensions', // like "img,png,jpg" 'files_path' => 'Files Path', // directory path to save files 'manager_class' => 'manager class', // class to manage files if empty it will use \vatandoost\filemanager\libs\BaseManager 'handler_type' => 'type of handler class', // 1 for local handler 2 for Ftp Handler 'has_public_thumb' => 'public thumbnail', // if files are private they can have public thumbnail for performance 'has_force_relation_id' => 'force to has relation id to work with filemanager',
管理类始终是一个可以更改文件管理器某些行为(模型)的模型。
您可以在下面的类中查看可用选项,并且可以将其中任何一个定义到您的模型中
\vatandoost\filemanager\libs\BaseManager
- filterQueryFiles($query, $fileType, $relation_id = null)
- canView($file)
- canDelete($file)
- beforeFileDelete($file)
- afterFileDelete($file)
- fileTypeConfig($fileType, $relationId = null)
文件管理器 UI 是一个 iframe,用于显示要选择或管理的文件。您可以使用它作为一个独立的页面,也可以将其添加到您的页面中
打开文件管理器的选项:"filemanager/dialog/index"
- unique_name:它是 file_type_name 和 relation_id 的组合,用 ',' 连接在一起
- selector:如果发送 selector=off,则禁用选择文件,仅显示它们
- callback:父窗口中要调用的函数名称
- field_id:将其值放入其输入 id 中的输入 id
- multiple:(0/1)它显示是否可以选择多个文件(默认:1)。例如
用法
您可以在不使用 UI 和选择器的情况下使用此模块。它可以帮助您轻松上传和访问文件
您需要将 fileTrait 添加到您的模型中,然后定义您的属性
示例模型(管理类)
class TestFile extends \yii\db\ActiveRecord { use \vatandoost\filemanager\libs\FileTrait; public $file; public static function tableName() { return 'test_file'; } public function rules() { return [ [ ['file'], 'vatandoost\filemanager\libs\FileValidator', // you have to use this file validator or extend of that 'file_type_name' => '', // if you did not define this it will complete based on class name 'relation_field' => 'id', // use this attribute to save in relation field in files table //'target_field' => 'file_id', // file id will save in this attribute after save if multiple false 'thumb' => [250, 250], // thumbnail size if files are image 'multiple' => true // we can add multiple files for this field // you can define any other \yii\validators\FileValidator properties here ], // other attributes //[['file_id'], 'integer'], [['name'], 'string', 'max' => 255], [['name'], 'required'], [['files_str'], 'string', 'max' => 255], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Name', //'file_id' => 'File ID', 'files_str' => 'File list', ]; } }
对于在 Yii2 中上传文件,您只需运行此函数即可
$model->saveFiles()
示例控制器中文件上传的使用
public function actionCreate() { $model = new TestFile(); if ($model->load(Yii::$app->request->post()) && $model->save() && $model->saveFiles()) { return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', [ 'model' => $model, ]); }
示例文件上传输入的使用
echo $form->field($model, 'file')->fileInput();
示例文件选择器的使用
echo $form->field($model, 'files_str')->widget(\vatandoost\filemanager\widgets\selector\Selector::class, [ 'fileTypeName' => 'frontend_models_testfile', 'relationId' => $model->id, 'multiple' => true ]);
还有一些其他功能可以使用
- $model->getFiles($attribute, $onlyModels = false) // 从模型获取所有文件
- $model->filesQuery($file_type_name = null) // 获取ActiveQuery类以与其他表合并
还有一个辅助类。您可以使用它的静态方法来处理您的应用程序中的文件
\vatandoost\filemanager\libs\FileHelper
- FileHelper::getFileUrl($file_id, $thumb = false)
- FileHelper::getFileInfo($file_id)
- FileHelper::getFileInfoByModel(File $file)
- FileHelper::deleteFile($file_id, $checkPermissions = true)
- FileHelper::renameFile($file_id, $new_name)
- FileHelper::deleteFileByModel(File $model, $checkPermissions = true)
- FileHelper::getFileContent($id = null, $model = null)
- FileHelper::getLocalFilePath($file, $fileType = null)
控制台命令
安装模块后,您可以使用以下命令来管理文件类型
#list of file types
php yii filemanager/files/types
# create new file type
php yii filemanager/files/create-type