Skip to content

Fix coding style #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
* A convenience class for temporary files.
*
* @author Michael Härtl <[email protected]>
* @version 1.1.0
* @license http://www.opensource.org/licenses/MIT
*/
class File
{
/**
* @var bool whether to delete the tmp file when it's no longer referenced or when the request ends.
* Default is `true`.
* @var bool whether to delete the tmp file when it's no longer referenced
* or when the request ends. Default is `true`.
*/
public $delete = true;

Expand All @@ -28,22 +27,24 @@ class File
*
* @param string $content the tmp file content
* @param string|null $suffix the optional suffix for the tmp file
* @param string|null $prefix the optional prefix for the tmp file. If null 'php_tmpfile_' is used.
* @param string|null $directory directory where the file should be created. Autodetected if not provided.
* @param string|null $prefix the optional prefix for the tmp file. If null
* 'php_tmpfile_' is used.
* @param string|null $directory directory where the file should be
* created. Autodetected if not provided.
*/
public function __construct($content, $suffix = null, $prefix = null, $directory = null)
{
if ($directory===null) {
if ($directory === null) {
$directory = self::getTempDir();
}

if ($prefix===null) {
if ($prefix === null) {
$prefix = 'php_tmpfile_';
}

$this->_fileName = tempnam($directory,$prefix);
if ($suffix!==null) {
$newName = $this->_fileName.$suffix;
if ($suffix !== null) {
$newName = $this->_fileName . $suffix;
rename($this->_fileName, $newName);
$this->_fileName = $newName;
}
Expand All @@ -63,9 +64,11 @@ public function __destruct()
/**
* Send tmp file to client, either inline or as download
*
* @param string|null $filename the filename to send. If empty, the file is streamed inline.
* @param string|null $filename the filename to send. If empty, the file is
* streamed inline.
* @param string $contentType the Content-Type header
* @param bool $inline whether to force inline display of the file, even if filename is present.
* @param bool $inline whether to force inline display of the file, even if
* filename is present.
*/
public function send($filename = null, $contentType, $inline = false)
{
Expand All @@ -78,10 +81,10 @@ public function send($filename = null, $contentType, $inline = false)
// #84: Content-Length leads to "network connection was lost" on iOS
$isIOS = preg_match('/i(phone|pad|pod)/i', $_SERVER['HTTP_USER_AGENT']);
if (!$isIOS) {
header('Content-Length: '.filesize($this->_fileName));
header('Content-Length: ' . filesize($this->_fileName));
}

if ($filename!==null || $inline) {
if ($filename !== null || $inline) {
$disposition = $inline ? 'inline' : 'attachment';
header(
'Content-Disposition: ' . $disposition .
Expand Down Expand Up @@ -117,7 +120,11 @@ public static function getTempDir()
{
if (function_exists('sys_get_temp_dir')) {
return sys_get_temp_dir();
} elseif ( ($tmp = getenv('TMP')) || ($tmp = getenv('TEMP')) || ($tmp = getenv('TMPDIR')) ) {
} elseif (
($tmp = getenv('TMP')) ||
($tmp = getenv('TEMP')) ||
($tmp = getenv('TMPDIR'))
) {
return realpath($tmp);
} else {
return '/tmp';
Expand Down