使用 php is_dir('~/tmp') 在一台机器上有效,但在另一台机器上无效。为什么?
我正在尝试运行以下代码:
is_dir('~/tmp');
在共享 LAMP 堆栈上。它工作正常,并返回 TRUE。 (该目录存在。)当我在本地机器(Mac OSX 10.5,运行 Zend Server Community Ed)上运行相同的代码时,我得到 FALSE,这是错误的,因为 ~/tmp 存在并且权限设置为 777。
我缺少服务器我想,在某个地方有指令。
我已经检查了 phpInfo 并且我(在本地和生产上):
safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
open_basedir no value no value
所以我认为我错过了一些东西,但是什么呢?
[编辑...]一些更多信息...
在本地运行以下命令
get_current_user()
给我“用户名”,这是我想要验证其〜/ tmp目录的正确用户,但
shell_exec('whoami')
给我“守护进程”。所以我想我知道我的问题来自哪里。现在我只需要弄清楚是否/如何更改在本地运行 Web 服务器的用户。
I am trying to run the following code:
is_dir('~/tmp');
On a shared LAMP stack. It works fine, and returns TRUE. (That directory exists.) When I run the same code on my local box (Mac OSX 10.5, running Zend Server Community Ed) I get FALSE which is wrong because ~/tmp exists and has permissions set to 777.
I am missing a server directive somewhere, I think.
I have checked with phpInfo and I have (on both local and production):
safe_mode Off Off
safe_mode_exec_dir no value no value
safe_mode_gid Off Off
safe_mode_include_dir no value no value
open_basedir no value no value
So I think that I am missing something, but what?
[edit...] Some more information...
running the following on my local
get_current_user()
gives me 'username', which is the correct user whose ~/tmp directory I want to verify, BUT
shell_exec('whoami')
gives me 'daemon'. So I think I know where my problem is coming from. Now I just need to figure out if/how I can change the user that is running the web server on my local.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否希望
~
扩展到您的主目录?我不愿意依赖 PHP 内部的这一点。 (刚刚在我的 Mac 上测试过,它没有扩展。)如果可能,尝试将
~/tmp
更改为完整路径名(例如,/Users/meriial/ tmp
)。更新:或者,您可以将
~
替换为$_ENV['HOME']
,如下所示:理想情况下,您应该检查
array_key_exists('HOME',$_ENV)
首先返回TRUE
,如果没有返回,则采取一些适当的操作(例如使用系统临时目录)。就此而言,正如 @xmarcos 指出的那样,无论使用 sys_get_temp_dir() 和 tempnam() ,您都可以只使用系统临时目录。这可能是最便携的,因此也是您的最佳选择。我认为您也可以通过这种方式创建原子临时文件,因此它可能更安全并且不太容易出现竞争条件。
Are you expecting
~
to be expanded to your home directory? I would be reluctant to rely on that inside of PHP. (Just tested it on my Mac, and it did not expand.)If possible, try changing
~/tmp
to whatever the full path name is (e.g., something like/Users/meriial/tmp
).UPDATE: Alternatively, you could replace
~
with$_ENV['HOME']
as follows:Ideally, you'd check that
array_key_exists('HOME',$_ENV)
returnsTRUE
first and take some appropriate action (like use the system temp dir) if it doesn't.For that matter, as @xmarcos points out, you could just use the system temp dir regardless using
sys_get_temp_dir()
andtempnam()
. That may be the most portable and therefore your best choice. I think you can also do atomic temp file creation that way, so it may be more secure and less prone to race conditions.您确定该目录存在于您的用户目录内部吗?
转到
终端
,然后输入cd ~/tmp
。有用吗?更新:您可以使用
sys_get_temp_dir
(如果可用),代码示例:Are you sure that directory existes inside your user directory?
Go to
Terminal
, and typecd ~/tmp
. Does is work?Update: you could use the
sys_get_temp_dir
if available, code example:对于所有虚拟路径,请先尝试扩展它们。做:
然后看看会发生什么,
然后尝试
opendir()
它并查看它出现什么错误。for all the virtual paths try to expand them first. do:
and then see what happens,
and then try to
opendir()
it and see what error it gets.