7
7
* A convenience class for temporary files.
8
8
*
9
9
* @author Michael Härtl <[email protected] >
10
- * @version 1.1.0
11
10
* @license http://www.opensource.org/licenses/MIT
12
11
*/
13
12
class File
14
13
{
15
14
/**
16
- * @var bool whether to delete the tmp file when it's no longer referenced or when the request ends.
17
- * Default is `true`.
15
+ * @var bool whether to delete the tmp file when it's no longer referenced
16
+ * or when the request ends. Default is `true`.
18
17
*/
19
18
public $ delete = true ;
20
19
@@ -28,22 +27,24 @@ class File
28
27
*
29
28
* @param string $content the tmp file content
30
29
* @param string|null $suffix the optional suffix for the tmp file
31
- * @param string|null $prefix the optional prefix for the tmp file. If null 'php_tmpfile_' is used.
32
- * @param string|null $directory directory where the file should be created. Autodetected if not provided.
30
+ * @param string|null $prefix the optional prefix for the tmp file. If null
31
+ * 'php_tmpfile_' is used.
32
+ * @param string|null $directory directory where the file should be
33
+ * created. Autodetected if not provided.
33
34
*/
34
35
public function __construct ($ content , $ suffix = null , $ prefix = null , $ directory = null )
35
36
{
36
- if ($ directory ===null ) {
37
+ if ($ directory === null ) {
37
38
$ directory = self ::getTempDir ();
38
39
}
39
40
40
- if ($ prefix ===null ) {
41
+ if ($ prefix === null ) {
41
42
$ prefix = 'php_tmpfile_ ' ;
42
43
}
43
44
44
45
$ this ->_fileName = tempnam ($ directory ,$ prefix );
45
- if ($ suffix !==null ) {
46
- $ newName = $ this ->_fileName . $ suffix ;
46
+ if ($ suffix !== null ) {
47
+ $ newName = $ this ->_fileName . $ suffix ;
47
48
rename ($ this ->_fileName , $ newName );
48
49
$ this ->_fileName = $ newName ;
49
50
}
@@ -63,9 +64,11 @@ public function __destruct()
63
64
/**
64
65
* Send tmp file to client, either inline or as download
65
66
*
66
- * @param string|null $filename the filename to send. If empty, the file is streamed inline.
67
+ * @param string|null $filename the filename to send. If empty, the file is
68
+ * streamed inline.
67
69
* @param string $contentType the Content-Type header
68
- * @param bool $inline whether to force inline display of the file, even if filename is present.
70
+ * @param bool $inline whether to force inline display of the file, even if
71
+ * filename is present.
69
72
*/
70
73
public function send ($ filename = null , $ contentType , $ inline = false )
71
74
{
@@ -78,10 +81,10 @@ public function send($filename = null, $contentType, $inline = false)
78
81
// #84: Content-Length leads to "network connection was lost" on iOS
79
82
$ isIOS = preg_match ('/i(phone|pad|pod)/i ' , $ _SERVER ['HTTP_USER_AGENT ' ]);
80
83
if (!$ isIOS ) {
81
- header ('Content-Length: ' . filesize ($ this ->_fileName ));
84
+ header ('Content-Length: ' . filesize ($ this ->_fileName ));
82
85
}
83
86
84
- if ($ filename !==null || $ inline ) {
87
+ if ($ filename !== null || $ inline ) {
85
88
$ disposition = $ inline ? 'inline ' : 'attachment ' ;
86
89
header (
87
90
'Content-Disposition: ' . $ disposition .
@@ -117,7 +120,11 @@ public static function getTempDir()
117
120
{
118
121
if (function_exists ('sys_get_temp_dir ' )) {
119
122
return sys_get_temp_dir ();
120
- } elseif ( ($ tmp = getenv ('TMP ' )) || ($ tmp = getenv ('TEMP ' )) || ($ tmp = getenv ('TMPDIR ' )) ) {
123
+ } elseif (
124
+ ($ tmp = getenv ('TMP ' )) ||
125
+ ($ tmp = getenv ('TEMP ' )) ||
126
+ ($ tmp = getenv ('TMPDIR ' ))
127
+ ) {
121
128
return realpath ($ tmp );
122
129
} else {
123
130
return '/tmp ' ;
0 commit comments