sokil / file-storage-bundle
0.1.10
2016-09-15 21:07 UTC
Requires
- php: >=5.5
- doctrine/doctrine-bundle: ~1.2
- doctrine/orm: ~2.2,>=2.2.3
- knplabs/knp-gaufrette-bundle: ~0.2
Requires (Dev)
- phpunit/phpunit: 3.7.*
Suggests
- sokil/php-upload: 0.*
This package is auto-updated.
Last update: 2024-08-28 01:21:18 UTC
README
与不同的文件系统协同工作,管理 Doctrine ORM 中的文件元数据。存储元数据可以通过检查文件哈希值来防止重复加载相同的文件。
安装
添加 composer 依赖项
composer require sokil/file-storage-bundle
将捆绑包添加到 AppKernel
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Knp\Bundle\GaufretteBundle\KnpGaufretteBundle(), new Sokil\FileStorageBundle\FileStorageBundle(), ); } }
配置
- 在 https://github.com/KnpLabs/Gaufrette 上了解关于 Gaufrette 的信息。
- 在 https://github.com/KnpLabs/KnpGaufretteBundle 上了解如何在 Symfony 中配置 Gaufrette 文件系统。
支持的文件系统的配置
此捆绑包使用 gaufrette 作为文件系统抽象,因此您需要在应用程序配置中配置文件系统。
knp_gaufrette: adapters: acme.attachments_adapter: local: directory: "%kernel.root_dir%/attachments" create: true filesystems: acme.attachments_filesystem: adapter: acme.attachments_adapter
然后,您需要将此文件系统名称传递给您的代码。例如,在您的捆绑包的扩展中定义参数。
<?php namespace AcmeBundle\DependencyInjection; class AcmeExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { if (isset($config['attachments_filesystem'])) { $container->setParameter( $this->getAlias() . '.attachments_filesystem', $config['attachments_filesystem'] ); } } }
然后在应用程序配置中
acme: attachments_filesystem: "acme.attachments_filesystem"
现在,您可以在控制器中使用已配置的文件系统。
<?php $attachmentFilesystem = $this ->get('knp_gaufrette.filesystem_map') ->get($this->getParameter('acme.attachments_filesystem'));
将本地文件移动到文件系统
此捆绑包非常有用,可以将本地文件移动到某些外部文件系统,并在数据库中添加关于文件的记录。首先,我们需要创建一些文件实体。文件实体保存有关存储文件的有用元数据。
<?php $file = new File(); $file ->setName('some.txt') ->setSize(4242) ->setCreatedAt(new \DateTime()) ->setMime('text/plain') ->setHash('some_hash_of_file_content'); $this ->get('file_storage') ->write( $file, 'acme.attachments_filesystem', 'some content of file' ); $fileId = $file->getId(); // now you have id of file
参见
- 在 https://github.com/sokil/php-upload 上了解如何轻松处理上传的文件。