承接 vldmir/laravel-temp-file-manager 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vldmir/laravel-temp-file-manager

Composer 安装命令:

composer require vldmir/laravel-temp-file-manager

包简介

A Laravel package for managing temporary files with automatic cleanup

README 文档

README

A Laravel package for managing temporary files with automatic cleanup functionality. This package helps you manage temporary files in your Laravel application with features like automatic cleanup, file registration, and scheduled deletion of old files.

Latest Version on Packagist Total Downloads

Features

  • 🚀 Easy temporary file management
  • 💾 Multiple methods for saving temporary files
  • 🧹 Automatic cleanup of old files
  • 🗑️ Auto-deletion of registered files after process completion
  • ⚙️ Configurable storage location and retention period
  • 📦 Laravel integration with Facade support
  • 🔄 Scheduled cleanup command
  • 💪 Strong typing and modern PHP 8.0+ features

Requirements

  • PHP 8.0 or higher
  • Laravel 8.0 or higher

Installation

You can install the package via composer:

composer require vldmir/laravel-temp-file-manager

Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Vldmir\TempFileManager\TempFileManagerServiceProvider" --tag="config"

This will create a temp-file-manager.php config file in your config directory. You can modify these settings:

return [
    'directory' => 'temp',
    'max_age_hours' => 10,
    'disk' => 'local',
];

Usage

Saving Files

The package provides several methods to save files to temporary storage:

1. Save String Content

// Save string content
$content = "Hello, World!";
$tempPath = TempManager::save($content, 'hello.txt');

// Save with auto-generated filename
$tempPath = TempManager::save($content);

2. Save Uploaded Files

// In your controller
public function upload(Request $request)
{
    // Save uploaded file with original name
    $tempPath = TempManager::saveUploadedFile($request->file('document'));
    
    // Save with custom filename
    $tempPath = TempManager::saveUploadedFile(
        $request->file('document'), 
        'custom-name.pdf'
    );
}

3. Save From URL

// Download and save file from URL
try {
    $tempPath = TempManager::saveFromUrl('https://example.com/file.pdf');
    
    // With custom filename
    $tempPath = TempManager::saveFromUrl(
        'https://example.com/file.pdf', 
        'local-copy.pdf'
    );
} catch (\Exception $e) {
    // Handle download error
}

4. Save Stream/Resource

// Save from resource
$handle = fopen('path/to/file', 'r');
$tempPath = TempManager::save($handle, 'output.txt');
fclose($handle);

File Cleanup Behavior

There are three ways files can be cleaned up:

  1. Automatic Cleanup After Process (Using register)
// The file will be automatically deleted when the PHP process ends
$tempPath = TempManager::getTempPath('upload.txt');
TempManager::register($tempPath);

// Do your work with the file
// ...
// File will be deleted automatically after process completion
  1. Manual Cleanup (Using cleanup method)
// Manually delete when you're done
TempManager::cleanup($tempPath);
  1. Scheduled Cleanup (For old files)
// All files older than max_age_hours will be removed
TempManager::cleanupOldFiles();

Scheduled Cleanup Command

To automatically clean up old temporary files, register the cleanup command in your App\Console\Kernel:

protected function schedule(Schedule $schedule)
{
    $schedule->command('temp-files:cleanup')->hourly();
}

Dependency Injection Usage

You can also use dependency injection to access the TempFileManager:

use Vldmir\TempFileManager\TempFileManager;

class MyController extends Controller
{
    public function __construct(private TempFileManager $tempManager)
    {
        $this->tempManager = $tempManager;
    }

    public function store(Request $request)
    {
        $tempPath = $this->tempManager->getTempPath('uploaded-file.txt');
        $this->tempManager->register($tempPath);
        // File will be auto-cleaned after process ends
    }
}

Complete Example in Controller

class DocumentController extends Controller
{
    public function store(Request $request)
    {
        try {
            // Save uploaded file
            $tempPath = TempManager::saveUploadedFile($request->file('document'));
            
            // Process the file
            // ...
            
            // Optionally clean up early if you're done
            TempManager::cleanup($tempPath);
            
            return response()->json(['success' => true]);
            
        } catch (\Exception $e) {
            // File will be auto-cleaned up when process ends
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
    
    public function download()
    {
        try {
            // Save remote file temporarily
            $tempPath = TempManager::saveFromUrl('https://example.com/document.pdf');
            
            // Process or serve the file
            return response()->download($tempPath);
            // File will be cleaned up after response is sent
            
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}

File Naming Behavior

When saving files, the package handles filenames in the following way:

  1. If no filename is provided, a random name is generated
  2. If a filename is provided:
    • Unsafe characters are removed from the filename
    • If a file with the same name exists, a counter is appended (e.g., file_1.txt, file_2.txt)
// Examples of filename handling
$manager = app(TempFileManager::class);

// With custom filename (unsafe characters are removed)
$path = $manager->save($content, 'my/unsafe:file.txt');
// Results in: my_unsafe_file.txt

// With duplicate filename
$path1 = $manager->save($content1, 'report.pdf');
$path2 = $manager->save($content2, 'report.pdf');
// Results in: report.pdf, report_1.pdf

// With uploaded file
$path = $manager->saveUploadedFile($uploadedFile, 'custom-name.pdf');
// Uses the provided name, sanitized if necessary

// Without filename
$path = $manager->save($content);
// Generates a random filename

The filename sanitization process:

  • Removes any character that isn't alphanumeric, dot, dash, or underscore
  • Replaces multiple consecutive dots/underscores with a single one
  • Removes dots and dashes from the start and end of the filename
  • Ensures the filename isn't empty

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email your.email@example.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

vldmir/laravel-temp-file-manager 适用场景与选型建议

vldmir/laravel-temp-file-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 vldmir/laravel-temp-file-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vldmir/laravel-temp-file-manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 vldmir/laravel-temp-file-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-15