2

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.

meriial
  • 4,946
  • 2
  • 23
  • 21

3 Answers3

5

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.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • Yes, I am expecting ~ to expand into my home directory. It works on the shared hosting linux machine, but not on my mac. The challenge is that I would like to write to ~/tmp without having different code on the production and development machines. – meriial Oct 30 '11 at 02:55
  • Instead of using `~`, you could use the `$HOME` environment variable. – Trott Oct 30 '11 at 04:09
  • I should have been less confident that ~ was expanding to the home directory on my production server. (Because it wasn't.) Can we say with confidence that php _never_ expands ~ to the home directory? – meriial Oct 30 '11 at 07:47
  • 1
    I believe tilde expansion is a function of the shell and I see no documentation suggesting PHP replicates it. I wouldn't say with confidence that it never does it, but I strongly suspect that to be true. – Trott Oct 30 '11 at 15:28
3

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'
xmarcos
  • 3,298
  • 2
  • 20
  • 27
  • 1
    +1 for `sys_get_temp_dir`. The issue is likely though that PHP runs as a different user, so `~` in PHP and `~` in the Terminal are two different directories. – deceze Oct 30 '11 at 03:53
2

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.

meriial
  • 4,946
  • 2
  • 23
  • 21
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • ah ha! good advice. it turns out that on my production server, is_dir('~/tmp') was returning TRUE because the directory /path/to/working/directory/~/tmp existed, which I discovered after using realpath(). Because ~/tmp also existed, I had assumed that it was expanding ~ to the home directory, but it wasn't. – meriial Oct 30 '11 at 07:37
  • does this mean that php can/will never expand ~ to a home directory? – meriial Oct 30 '11 at 07:39
  • @meriial php has very little to do here. PHP workson the system and if system expands ~ so PHP does. – Your Common Sense Oct 30 '11 at 09:00
  • when you say 'system', do you mean shell? The shell on both my production (linux) and development (mac) servers expands ~, but php realpath() and is_dir() do not. See Trott's comment above. – meriial Oct 30 '11 at 19:34