views:

630

answers:

2

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?

+2  A: 

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

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

Frank Farmer
+3  A: 

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.

Rob