Logo

Programming-Idioms

History of Idiom 139 > diff from v19 to v20

Edit summary for version 20 by I have none:
[PHP] The "explain" was wrong about the return value

Version 19

2020-04-08, 16:23:40

Version 20

2020-04-29, 19:37:24

Idiom #139 Create temp directory

Create a new temporary folder on filesystem, for writing.

Idiom #139 Create temp directory

Create a new temporary folder on filesystem, for writing.

Extra Keywords
tmp tmpdir tempdir folder
Extra Keywords
tmp tmpdir tempdir folder
Code
function createTempDir(): ?string
{
    $separator = DIRECTORY_SEPARATOR;
    $path = rtrim(sys_get_temp_dir(), $separator) . $separator . mt_rand() . microtime(true);
    $createdTempDir = mkdir($path);
    if ($createdTempDir) {
        return $path;
    }

    return null;
}
Code
function createTempDir(): ?string
{
    $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . mt_rand() . microtime(true);
    if (mkdir($path)) {
        return $path;
    }
    return null;
}
Comments bubble
returns the new directory's path or false in case of error
Comments bubble
returns the new directory's path or null in case of error
Doc URL
https://secure.php.net/manual/en/function.sys-get-temp-dir.php
Doc URL
https://secure.php.net/manual/en/function.sys-get-temp-dir.php
Demo URL
https://3v4l.org/1cca3
Demo URL
https://3v4l.org/1cca3