emmetog/temporary

允许轻松处理临时文件和目录,它们将被自动删除

v1.0.1 2017-01-22 14:23 UTC

This package is auto-updated.

Last update: 2024-08-29 03:58:12 UTC


README

允许轻松处理临时文件和目录,它们将被自动删除

安装

composer require emmetog/temporary

使用方法

临时文件

<?php

use Emmetog\Temporary\File;

$tempFile = new File();

// Returns the generated temporary filename.
$filename = $tempFile->getFilename();

file_put_contents($filename, 'This file is only temporary');

// When we remove all references to the object, the file is removed.
unset($tempFile);
// Or
$tempFile->remove();

您不需要调用 unset()->remove(),当脚本结束时,文件也将自动删除。

临时目录

<?php

use Emmetog\Temporary\Directory;

$tempDir = new Directory();

// Returns the generated temporary directory path.
$tempPath = $tempDir->getDirectory();

// Let's fill the directory with a file.
file_put_contents($tempPath . '/somefile.txt', 'This file is inside a temporary dir');

// When we remove all references to the object, the directory and
// all subdirectories are removed.
unset($tempDir);
// Or
$tempDir->remove();

您不需要调用 unset()->remove(),当脚本结束时,目录及其所有子目录将自动删除。

警告:在临时目录内创建的任何文件或目录,以及放入临时文件中的任何内容,在脚本结束时都将被完全删除。这是库的用途,因此请不要在其中放置任何重要内容。