PHP: How to set current working directory to be same as directory executing the script

蹲街弑〆低调 提交于 2019-11-26 21:09:49

问题


I'm in the process of transferring my website from one server to another. I have some php scripts that use the is_readable function which uses the current working directory.

On the old server, when I call getcwd(), it outputs the folder in which the script is being executed. On the new server it outputs the root directory '/'.

I would like to know how I can configure PHP to use the current folder instead of '/'. I don't want to have to change any PHP code that already works on the old server. I can configure the new server, but don't know what settings to change. I'm using apache2, if that helps.

EDIT: It seems as though my working directory is not root like I thought. When I create a testFile.php and echo getcwd() it shows the directory the php file is in. But in my problem file, in the same directory, getcwd() shows up as '/'


回答1:


chdir(__DIR__);

or

chdir(dirname(__FILE__));

(see chdir and magic constants).

But that should be by default.




回答2:


This is normal in CLI mode:

It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)

a quick workaround would be

chdir(dirname(__FILE__));



回答3:


You can get the current directory a script is in with dirname(__FILE__) or __DIR__ if >= PHP 5.3.



来源:https://stackoverflow.com/questions/5254000/php-how-to-set-current-working-directory-to-be-same-as-directory-executing-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!