Skip to content

A convenience class for temporary files

License

Notifications You must be signed in to change notification settings

lextira/php-tmpfile

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-tmpfile

GitHub Tests Packagist Version Packagist Downloads GitHub license

A convenience class for temporary files.

Features

  • Create temporary file with arbitrary content
  • Delete file after use (can be disabled)
  • Send file to client, either inline or with save dialog, optionally with custom HTTP headers
  • Save file locally

Examples

<?php
use mikehaertl\tmp\File;

$file = new File('some content', '.html');

// send to client for download
$file->send('home.html');
// ... with custom content type (autodetected otherwhise)
$file->send('home.html', 'application/pdf');
// ... for inline display (download dialog otherwhise)
$file->send('home.html', 'application/pdf', true);
// ... with custom headers
$file->send('home.html', 'application/pdf', true, [
    'X-Header' => 'Example',
]);

// save to disk
$file->saveAs('/dir/test.html');

// Access file name and directory
echo $file->getFileName();
echo $file->getTempDir();

In some places you might be forced to pass the file path instead of the instance, leading to the situation that the instance is no longer referenced. You can delay the automatic delete until the end of the request by calling keepDuringRequest().

<?php
use mikehaertl\tmp\File;

$file = new File('some content');
$file->keepDuringRequest();

If you want to keep the temporary file, e.g. for debugging, you can set the $delete property to false:

<?php
use mikehaertl\tmp\File;

$file = new File('some content', '.html');
$file->delete = false;

Default HTTP headers can also be added:

<?php
use mikehaertl\tmp\File;

File::$defaultHeader['X-Header'] = 'My Default';

$file = new File('some content', '.html');
$file->send('home.html');

About

A convenience class for temporary files

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%