windows 解决PHP中的Windows环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/923942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resolve Windows environment variables in PHP
提问by nickf
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:
我想写入 Windows 临时目录中的文件。我知道在命令行中您可以使用环境变量%TEMP%
来获取正确的路径,但是尝试执行以下操作:
file_put_contents("%TEMP%\myfile.txt");
...does not work because the environment variable isn't being resolved. Is there a way to do this?
...不起作用,因为环境变量没有被解析。有没有办法做到这一点?
回答by Rob
getenv('TEMP')
or $_ENV['temp']
getenv('TEMP')
或者 $_ENV['temp']
Incidentally, if you're working with temporary files, then you might want to look into the tempnam()
and tmpfile()
functions.
顺便说一句,如果您正在处理临时文件,那么您可能需要查看tempnam()
和tmpfile()
函数。
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.
前者会在任何目录中保留一个临时文件名(虽然你可以让它在系统临时目录中创建一个文件);后者实际上会创建一个临时文件并返回一个文件句柄资源,当句柄关闭时自动丢弃该临时文件。
回答by Frank Farmer
http://www.php.net/manual/en/function.getenv.php
http://www.php.net/manual/en/function.getenv.php
file_put_contents(getenv('TEMP') . DIRECTORY_SEPARATOR . "myfile.txt");
file_put_contents(getenv('TEMP') . DIRECTORY_SEPARATOR . "myfile.txt");