tourze/symfony-request-file-clean-bundle
Composer 安装命令:
composer require tourze/symfony-request-file-clean-bundle
包简介
A Symfony bundle that automatically cleans up temporary uploaded files after request completion
README 文档
README
A Symfony bundle that automatically cleans up temporary uploaded files after request completion in special environments where the system doesn't automatically remove them.
Features
- Automatically removes temporary uploaded files after request completion
- Works with both array-style file uploads and UploadedFile objects
- High priority event listener ensures cleanup happens after all other processes
- Zero configuration required
Installation
Install the bundle using Composer:
composer require tourze/symfony-request-file-clean-bundle
Quick Start
There's no configuration required. The bundle automatically registers the event subscriber that handles the file cleanup after request termination.
If you're using Symfony Flex, the bundle will be automatically registered. If not, you need to
add it to your config/bundles.php:
<?php return [ // ... Tourze\RequestFileCleanBundle\RequestFileCleanBundle::class => ['all' => true], ];
Configuration
This bundle works out of the box without any configuration. However, if you need to understand how it works internally:
- Event Priority: The bundle listens to
KernelEvents::TERMINATEwith priority-9999 - File Types: Supports both array-style uploads and
UploadedFileobjects - Cleanup Strategy: Files are removed using PHP's
unlink()function
Advanced Usage
Custom File Handling
While the bundle handles standard file uploads automatically, you can extend its functionality:
<?php use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\TerminateEvent; use Symfony\Component\HttpKernel\KernelEvents; class CustomFileCleanSubscriber implements EventSubscriberInterface { public function onTerminate(TerminateEvent $event): void { // Your custom cleanup logic here // This will run before the default cleanup } public static function getSubscribedEvents(): array { return [ KernelEvents::TERMINATE => ['onTerminate', -9998], // Higher priority ]; } }
Debugging File Cleanup
If you need to debug the file cleanup process, you can create a custom logger:
<?php use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\Event\TerminateEvent; class LoggingFileCleanSubscriber { public function __construct(private LoggerInterface $logger) {} public function onTerminate(TerminateEvent $event): void { foreach ($event->getRequest()->files->all() as $file) { $this->logger->info('Cleaning uploaded file', [ 'file' => is_array($file) ? $file['tmp_name'] ?? 'unknown' : $file->getPathname() ]); } } }
How It Works
The bundle registers an event subscriber that listens to the KernelEvents::TERMINATE event with
a very low priority (-9999). When the request terminates, it iterates through all uploaded files
in the request and properly removes the temporary files from the filesystem.
This helps prevent file system bloat in environments where temporary files aren't automatically cleaned up by the system.
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
Use Cases
This bundle is particularly useful in:
- Docker containers where temporary directories might not be automatically cleaned
- Shared hosting environments with limited file system cleanup
- Development environments where temporary files accumulate over time
- Production environments with custom file upload handling
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This bundle is released under the MIT License. See the License File for more information.
tourze/symfony-request-file-clean-bundle 适用场景与选型建议
tourze/symfony-request-file-clean-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/symfony-request-file-clean-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/symfony-request-file-clean-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-30