1

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 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.