mikehaertl / php-tmpfile
A convenience class for temporary files
Installs: 19 993 978
Dependents: 24
Suggesters: 0
Security: 0
Stars: 96
Watchers: 1
Forks: 16
Open Issues: 3
Requires (Dev)
- php: >=5.3.0
- phpunit/phpunit: >4.0 <=9.4
This package is auto-updated.
Last update: 2024-10-14 16:14:17 UTC
README
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();
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');
The $ignoreUserAbort
option (on by default) mitigates an issue where the file
was not deleted if the user closes the connection during a download. Try setting
it to false
if you experience unexpected behavior.