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

3

25

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

Fasciculus answered 10/3, 2011 at 0:15 Comment(3)
The two different servers are running different versions of php, right?Incursive
Yes: Old server uses 5.2. New server uses 5.3.Fasciculus
Possible duplicate of In PHP, Best way to ensure current working directory is same as script , when using CLILangsdon
C
34

chdir(__DIR__);

or

chdir(dirname(__FILE__));

(see chdir and magic constants).

But that should be by default.

Calliopsis answered 10/3, 2011 at 0:17 Comment(2)
Thanks, but can I put this in php.ini or some config file? I do not want to edit my existing php code.Fasciculus
@Fasciculus 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 <?php $config['settingName'] = __DIR__ . 'someValue';)Survivor
B
9

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__));
Bluestocking answered 10/3, 2011 at 0:18 Comment(3)
Thanks. Do you know how I can switch out of CLI mode?Fasciculus
@Fasciculus why are you in it in the first place? Where is your script running?Bluestocking
I'm not sure that I am. My script is running on apache, in my www folder.Fasciculus
E
2

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

Entero answered 10/3, 2011 at 0:18 Comment(5)
+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.Manama
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.Fasciculus
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.Manama
I can control the configuration of the new machine.Fasciculus
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!Nightmare

© 2022 - 2024 — McMap. All rights reserved.