codeinc/temp-file

This package is abandoned and no longer maintained. No replacement package was suggested.

A simple PHP 7 class to manage temporary files

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 is a simple PHP class to manage temporary files. The temporary file is created using tempnam(). It self-destructs when the class is destructed. By default the file is created in the system temp directory obtained using sys_get_temp_dir().

Usage

<?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

You can also create a non self-destructive 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); 

Installation

This library is available through Packagist and can be installed using Composer:

composer require codeinc/temp-file

License

The library is published under the MIT license (see LICENSE file).