using php is_dir('~/tmp') works on one machine, but not on another. Why?
-
16-02-2021 - |
Question
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.
Solution
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:
is_dir($_ENV['HOME'] . '/tmp');
Ideally, you'd check that array_key_exists('HOME',$_ENV)
returns TRUE
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()
and tempnam()
. 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.
OTHER TIPS
Are you sure that directory existes inside your user directory?
Go to Terminal
, and type cd ~/tmp
. Does is work?
Update: you could use the sys_get_temp_dir
if available, code example:
$some_log = tempnam(sys_get_temp_dir(), 'some_log');
var_dump($some_log);
// will return '/private/var/folders/.../some_logbqzDvg'
for all the virtual paths try to expand them first. do:
var_dump(realpath('~/tmp'));
and then see what happens,
and then try to opendir()
it and see what error it gets.