Hi,

Does anyone know how to get the system temp folder path? I am looking for a function that will return something like "/tmp" or "C:\Windows\Temp".

Thanks.

    Hi,

    this is sort of a hack but it should work (not every OS has an environment variable called TMP or TEMP):

    <?PHP
    $tmpfile = tempnam("dummy","");
    $path = dirname($tmpfile);
    echo $path;
    unlink($tmpfile);
    ?>
    

    When you use tempnam with a directory that doesn't exist (like e.g. dummy) PHP will create a temporary file in the system's default temporary directory and return the absolute path to the file. Extract the path information with dirname and you have the default temporary path.

    Thomas

      2 years later

      Sorry to dig up an old thread, but this is exactly what I was looking for.

      Do you know how reliable this is? ie: How many servers DO NOT have a temp folder system set up that could be utilized by php?

      I'm trying to unzip a .zip file into a temp folder... but I'm running into permissions issues on safe_mode systems... so now I am trying to find a better solution.

      (My thinking is to use whatever "temp" facility is offered by php...)

      What do you think?

        Even when running with safe_mode or open_basedir in effect, the sensible* PHP administrator will have allowed you write access to a temporary directory somewhere. Even if it's a private one per-user.

        Mark

        • This is something of an assumption 🙂

          Temp directories are usually chmod'd to 777, are they not?

          On my Windows server, here's the results:

          echo tempnam(NULL, 'abc'); // \abcE9.tmp (in the root of my C:\)
          echo tempnam('bradg', 'abc'); // C:\WINDOWS\TEMP\abcE9.tmp

          On my Unix server...

          echo tempnam(NULL, 'abc'); // /tmp/abc7jSD7o
          echo tempnam('bradg3', 'abc'); // /tmp/abcAPLcG1
            Write a Reply...