This repository has been archived by the owner. It is now read-only.
A simple PHP library to create temporary files
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

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


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:

use CodeInc\TempFile\TempFile;

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

// will return TRUE, the temp file is NOT deleted by the class destructor


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

composer require codeinc/temp-file


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