zenstruck/temp-file 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

zenstruck/temp-file

最新稳定版本:v1.3.0

Composer 安装命令:

composer require zenstruck/temp-file

包简介

Temporary file wrapper.

README 文档

README

CI Status codecov

Temporary file wrapper. Created files are deleted at the end of the script.

Installation

composer require zenstruck/temp-file

API

Create TempFile's

use Zenstruck\TempFile;

// create empty file with random name (in /tmp)
$file = TempFile::new();

// create empty file with random filename, with extension (in /tmp)
$file = TempFile::withExtension('txt');

// create file with specific filename (in /tmp)
$file = TempFile::withName('my-file.txt'); // creates empty file
$file = TempFile::withName('my-file.txt', 'some content'); // creates file with string content
$file = TempFile::withName('my-file.txt', \fopen('some/file.txt', 'r')); // creates file with resource as content
$file = TempFile::withName('my-file.txt', new \SplFileInfo('some/file.txt')); // creates file from existing file (existing file is copied)

// create for existing file
$file = TempFile::new('some/file.txt'); // note: will be deleted at the end of the script

// create with string content
$file = TempFile::for('some contents');

// create with resource
$file = TempFile::for(\fopen('some/file.txt', 'r'));

// create from another file (existing file is copied)
$file = TempFile::for(new \SplFileInfo('some/file.txt'));

// create image
$image = TempFile::image(); // temporary 10x10 image with 'jpg' as the extension
$image = TempFile::image(100, 50); // customize the dimensions
$image = TempFile::image(type: 'gif'); // customize the image type
$image = TempFile::image(name: 'my-image.png'); // customize the file name

Using TempFile's

/** @var \Zenstruck\TempFile $file */

$file->contents(); // string - the file's contents
$file->refresh(); // self - clearstatcache() on the file (refreshes metadata)
$file->delete(); // self - delete the file

// is instance of \SplFileInfo
$file->getMTime(); // int - last modified timestamp
$file->getExtension(); // string - file extension

Long-Running Processes

Created TempFile's are automatically deleted at the end of the script by keeping track of created files and purging with register_shutdown_function. If using a long-running PHP process (like a worker or Swoole/RoadRunner runtime) the files will not be purged until the process is stopped. This creates a memory leak as the tracked created files grows in memory. To combat this, you'll need to hook into some kind of event in your process that enables you to clear these type of leaks and call TempFile::purge() manually.

Symfony Integration

A simple service is provided to purge TempFile's at the end of each request and, if using symfony/messenger, after a job is processed.

To use, register the service:

# config/packages/zenstruck_temp_file.yaml

services:
    Zenstruck\TempFile\Bridge\Symfony\PurgeTempFiles:
        autoconfigure: true

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-05