在PHP中解析Windows环境变量

时间:2022-10-28 23:06:01

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?

...不起作用,因为环境变量没有得到解决。有没有办法做到这一点?

2 个解决方案

#1


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.

前者将在任何目录中保留一个临时文件名(尽管您可以在系统临时目录中创建一个文件);后者实际上会创建一个临时文件并返回一个文件句柄资源,当句柄关闭时自动丢弃临时文件。

#2


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”);

#1


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.

前者将在任何目录中保留一个临时文件名(尽管您可以在系统临时目录中创建一个文件);后者实际上会创建一个临时文件并返回一个文件句柄资源,当句柄关闭时自动丢弃临时文件。

#2


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”);