MikeHaertl/php-tmpfile

临时文件的便捷类

1.2.1 2021-03-01 18:26 UTC

README

GitHub Tests Packagist Version Packagist Downloads GitHub license

临时文件的便捷类。

功能

  • 创建具有任意内容的临时文件
  • 使用后删除文件(可禁用)
  • 将文件发送到客户端,可以是内联或带有保存对话框,可选地带有自定义HTTP头
  • 本地保存文件

示例

<?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();

如果您想保留临时文件,例如用于调试,可以将$delete属性设置为false

<?php
use mikehaertl\tmp\File;

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

还可以添加默认HTTP头

<?php
use mikehaertl\tmp\File;

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

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