23

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 '/'

MF86
  • 255
  • 1
  • 3
  • 8
  • The two different servers are running different versions of php, right? – klang Mar 10 '11 at 00:28
  • 1
    Yes: Old server uses 5.2. New server uses 5.3. – MF86 Mar 10 '11 at 00:52
  • Possible duplicate of [In PHP, Best way to ensure current working directory is same as script , when using CLI](http://stackoverflow.com/questions/192092/in-php-best-way-to-ensure-current-working-directory-is-same-as-script-when-us) – George Marian Sep 03 '16 at 18:22

3 Answers3

31

chdir(__DIR__);

or

chdir(dirname(__FILE__));

(see chdir and magic constants).

But that should be by default.

Czechnology
  • 14,253
  • 9
  • 58
  • 83
  • 1
    Thanks, but can I put this in php.ini or some config file? I do not want to edit my existing php code. – MF86 Mar 10 '11 at 00:20
  • @MF86 You cannot put it in your php.ini. But you can put it in config files that are written as plain PHP code. (EG a config file containing code such as ` – nl-x Dec 21 '17 at 10:29
7

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__));
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
2

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

alex
  • 438,662
  • 188
  • 837
  • 957
  • +1 for not suggesting `chdir`, which can have unwanted side effects. It will be better to adjust the script to not care what the current working directory is than to continue the possibly weird behavior. – Charles Mar 10 '11 at 00:20
  • I would like to be able to do this without changing the existing scripts. On the old server getcwd() returns the current directory executing script. – MF86 Mar 10 '11 at 00:25
  • If you need your code to work on machines on which you can not control the configuration, you will *need* to change your code to simply not care about the external configuration. `__DIR__` or `dirname(__FILE__)` is guaranteed to get you the directory in which the file lives without caring about the cwd. – Charles Mar 10 '11 at 00:29
  • I can control the configuration of the new machine. – MF86 Mar 10 '11 at 00:52
  • Wow! This solved my entire issue on IIS!! I had the error mage.php not found. Added this in index.php define('MAGENTO_ROOT', __DIR__); Thanks! – Flo Sep 30 '13 at 06:24