I'm in the process of migrating a site from one server to another. On the old server, getcwd() returns the directory tha开发者_JAVA百科t the script is running, all of the time. On the new server, in one of my scripts, once it goes inside a function, getcwd() returns root. Outside the function though, it returns the correct directory. Could this be due to the different server APIs being used? (old server uses CGI/FastCGI, new on uses Apache 2.0 handler)
EDIT: I'm actually not relying on getcwd() in the code, but is_readable(). I'm just checking using getcwd() because I was told is_readable() (which isn't working) relies on the cwd
If you want to get the directory in which the current script resides, it's usually better to use dirname(realpath(__FILE__))
rather than getcwd()
.
__FILE__ is one of several "magic constants".
Yes, it could be due to a number of things, including differences in the way that Apache and PHP are installed, API used, PHP version, and OS. At least one of these things is not consistent from server to server. It's also possible (if not probable) that whatever is happening in the function in question is what's causing the change in behavior (though that wouldn't explain why that didn't happen on the old server).
You probably shouldn't rely on the output of getcwd()
if you're going to deploy across several servers (or if you switch servers).
The behavior of your new server is odd, but I don't see why it would be a problem.
Ah, you didn't mention the switch from FastCGI to mod_php in your previous question! That's probably at fault -- Apache is starting in the root directory, while FastCGI does a directory change.
Have you considered just switching back to FastCGI? PHP 5.3.3+'s FPM SAPI is supposed to be pretty darn good, if you switched to mod_php because FastCGI was giving you trouble. (Edit: Ah, I see you've asked how in a different question. Unfortunately I've never set up FPM, only reaped the benefits...)
Otherwise the previous answers still apply: your code is far better off using __DIR__
or dirname(__FILE__)
than relying on the cwd. Right now your code is broken by doing this. You've probably spent more effort trying to find a workaround than it'd take to just fix it by now. :)
精彩评论