nickf
nickf

Reputation: 546045

Resolve Windows environment variables in PHP

I want to write to a file in the windows temp directory. I know that in the command line you can use the environment variable %TEMP% to get the right path, however trying to do something like this:

file_put_contents("%TEMP%\\myfile.txt");

...does not work because the environment variable isn't being resolved. Is there a way to do this?

Upvotes: 1

Views: 7622

Answers (2)

Rob
Rob

Reputation: 48369

getenv('TEMP') or $_ENV['temp']

Incidentally, if you're working with temporary files, then you might want to look into the tempnam() and tmpfile() functions.

The former will reserve a temporary file name in any directory (although you can have it create a file in the system temporary directory); the latter will actually create a temporary file and return a file handle resource, automatically discarding the temporary file when the handle is closed.

Upvotes: 6

Frank Farmer
Frank Farmer

Reputation: 39356

http://www.php.net/manual/en/function.getenv.php

file_put_contents(getenv('TEMP') . DIRECTORY_SEPARATOR . "myfile.txt");

Upvotes: 2

Related Questions