PHP中fopen和mkdir等函数不能使用~。~是shell中获取用户根目录的简写,在使用fopen和mkdir()函数时,往往不识别~符号。所以应该采用getenv函数获取根目录。
fopen(“~/www/1.txt, “wb”); //失败
getenv(“HOME”);
详细信息
http://php.net/manual/zh/function.getenv.php
同时,全局变量有$_ENV,但是需要确认php.ini是否开启:
; This directive determines which super global arrays are registered when PHP ; starts up. G,P,C,E & S are abbreviations for the following respective super ; globals: GET, POST, COOKIE, ENV and SERVER. There is a performance penalty ; paid for the registration of these arrays and because ENV is not as commonly ; used as the others, ENV is not recommended on productions servers. You ; can still get access to the environment variables through getenv() should you ; need to. ; Default Value: "EGPCS" ; Development Value: "GPCS" ; Production Value: "GPCS"; ; http://php.net/variables-order variables_order = "GPCS"
如果有E则可以使用$_ENV,官方不推荐开启E,所以一般线上是使用不了$_ENV的。
另外:
php mkdir 创建目录也不能使用~,同样使用mkdir(getenv(“HOME”).”/xxx/xxx/”, 0777, true);就可以递归创建多级目录