Get temporary dictionary in PHP

Inspired by:

If you want to get a temporary dictionary you could use sys_get_temp_dir(). But this doesn’t guarantee that you have write access to this dictionary. So, you can also get the upload_tmp_dir property from the php.ini file.

You can combine this for simplicity in a function:

    function get_temp():string{
        // Returns a temporary dictionary path.
        // When it can't find one, it returns false.
            return sys_get_temp_dir();
             return ini_get("upload_tmp_dir");
            return false;
    echo get_temp();

Or the short form:

    function get_temp():string|bool{
        return is_writable(sys_get_temp_dir())?sys_get_temp_dir():(is_writable(ini_get("upload_tmp_dir"))?ini_get("upload_tmp_dir"):false);
Written on March 3, 2022