A simple PHP library to create temporary files
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
LICENSE
README.md
composer.json

README.md

Temp file

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).