Skip to content

Commit bf3d34c

Browse files
committed
new: add new methods for get tempfile
1 parent 6be47c3 commit bf3d34c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/OS.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Toolkit\Stdlib;
1111

12+
use RuntimeException;
1213
use function defined;
1314
use function explode;
1415
use function function_exists;
@@ -25,6 +26,8 @@
2526
use function putenv;
2627
use function rtrim;
2728
use function stripos;
29+
use function tempnam;
30+
use function tmpfile;
2831
use const PHP_OS;
2932
use const PHP_OS_FAMILY;
3033

@@ -205,6 +208,31 @@ public static function getWorkDir(): string
205208
return (string)getcwd();
206209
}
207210

211+
/**
212+
* Creates a temporary file
213+
*
214+
* @return resource
215+
*/
216+
public static function newTempFile()
217+
{
218+
$fh = tmpfile();
219+
if ($fh === false) {
220+
throw new RuntimeException('create an temporary file fail');
221+
}
222+
223+
return $fh;
224+
}
225+
226+
/**
227+
* @param string $prefix
228+
*
229+
* @return string
230+
*/
231+
public static function tempFilePath(string $prefix = 'tmp_'): string
232+
{
233+
return tempnam(self::getTempDir(), $prefix);
234+
}
235+
208236
/**
209237
* @return string
210238
*/

src/Std.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
*/
1010
class Std
1111
{
12-
1312
}

0 commit comments

Comments
 (0)