codeinc/temp-file

该包已被废弃,不再维护。未建议替代包。

一个简单的PHP 7类,用于管理临时文件

1.0.0 2018-08-30 09:46 UTC

This package is auto-updated.

Last update: 2020-01-24 20:29:41 UTC


README

TempFile 是一个简单的PHP类,用于管理临时文件。临时文件使用 tempnam() 创建。类被销毁时,临时文件会自我销毁。默认情况下,文件创建在系统临时目录中,该目录通过 sys_get_temp_dir() 获取。

使用方法

<?php
use CodeInc\TempFile\TempFile;

// you can optionally specify a prefix and the parent directory to the constructor
$tempFile = new TempFile('my_temp_file_', '/tmp'); // creates the temp file
$tempFile->getPath(); // returns the temp file's path
(string)$tempFile; // equals to getPath(), returns the temp file's path
$tempFile->getSize(); // returns the temp file's size
$tempFile->getContents(); // returns the temp file's content
$tempFile->putContents(''); // set the temp file's content 
unset($tempFile); // deletes the temp file

您还可以创建一个非自我销毁的临时文件

<?php
use CodeInc\TempFile\TempFile;

$tempFile = new TempFile(null, null, false);
$tempFilePath = $tempFile->getPath();
unset($tempFile); 

// will return TRUE, the temp file is NOT deleted by the class destructor
file_exists($tempFilePath); 

安装

此库通过 Packagist 提供,并可以使用 Composer 安装。

composer require codeinc/temp-file

许可协议

该库在MIT许可协议下发布(见 LICENSE 文件)。