A simple library used to create temporary files and automatically delete them when they are no longer needed.
Find a file
2024-09-29 21:24:54 -04:00
.github/workflows Refactor build system and add CI pipeline 2024-09-29 21:06:03 -04:00
.idea Add PHPUnit tests for TempFile class 2024-09-29 21:21:48 -04:00
src/TempFile Refactor TempFile class field types and improve filename validation 2024-09-29 21:15:01 -04:00
tests/TempFile Add PHPUnit tests for TempFile class 2024-09-29 21:21:48 -04:00
.gitignore Updated .gitignore 2024-09-29 21:22:03 -04:00
.gitlab-ci.yml Initial Commit 2023-02-25 00:05:03 -05:00
bootstrap.php Add PHPUnit tests for TempFile class 2024-09-29 21:21:48 -04:00
CHANGELOG.md Bumped version & updated CHANGELOG.md 2024-09-29 21:24:54 -04:00
LICENSE Initial Commit 2023-02-25 00:05:03 -05:00
Makefile Refactor build system and add CI pipeline 2024-09-29 21:06:03 -04:00
phpunit.xml Add PHPUnit tests for TempFile class 2024-09-29 21:21:48 -04:00
project.json Bumped version & updated CHANGELOG.md 2024-09-29 21:24:54 -04:00
README.md Replaced $extension parameter with $options parameter in \TempFile > TempFile > __construct() so that it's possible to specify the file extension and the file name prefix. 2023-02-26 15:55:24 -05:00

TempFile

TempFile is a very simple library used for creating temporary files without having to write code to delete them once you're done with them.

Table of Contents

Installation

The library can be installed using ncc:

ncc install -p "nosial/libs.tempfile=latest@n64"

or by adding the following to your project.json file under the build.dependencies section:

{
  "name": "net.nosial.tempfile",
  "version": "latest",
  "source_type": "remote",
  "source": "nosial/libs.tempfile=latest@n64"
}

If you don't have the n64 source configured you can add it by running the following command:

ncc source add --name n64 --type gitlab --host git.n64.cc

Compiling from source

The library can be compiled from source using ncc:

ncc build --config release

or by running the following command:

make release

Usage

Just create a class object, optionally specifying options you'd like to use.

require_once('ncc');
import('net.nosial.tempfile');

$file1 = new TempFile\TempFile();
$file2 = new TempFile\TempFile([
    TempFile\Options::Extension => 'txt',
    TempFile\Options::Prefix => 'prefix_',
    TempFile\Options::Suffix => '_suffix',
    TempFile\Options::RandomLength => 8,
    TempFile\Options::Directory => '/tmp',
]);

You can obtain the file path by using the getFilepath() method or by using the object as a string.

echo $file1->getFilepath() . PHP_EOL;
file_put_contents($file2, 'Hello World!');

Files are automatically deleted when the object is destroyed, if for some reason the __destruct() method was not properly called, a shutdown function is automatically registered to delete all the temporary files that were created.

Options

The following options are available:

  • TempFile\Options::Extension - The file extension to use, defaults to 'tmp',
  • TempFile\Options::Prefix - The prefix to use for the file name, empty by default,
  • TempFile\Options::Suffix - The suffix to use for the file name, empty by default,
  • TempFile\Options::RandomLength - The length of the random string to use for the file name, defaults to 16,
  • TempFile\Options::Directory - The directory to create the file in, defaults to the system's temporary directory.
  • TempFile\Options::Filename - The filename to use, if specified, the random string will not be used.

License

This library is licensed under the MIT license, see the LICENSE file for more information.