Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 564
MF86 Avatar asked Mar 10 '11 00:03

MF86


People also ask

Which function used to identify the current working directory in PHP?

The getcwd() function is used to get the current working directory.

How do I change directories in PHP?

php | chdir() Function The chdir() function in PHP used to change PHP's current directory to new directory path. It takes only a single argument as new directory path. Parameters Used : This function accepts only one parameter and which is mandatory to be passed.

Which function returns the current working directory?

The pwd command will return the current working directory.

What is working directory in PHP?

The getcwd() function in PHP is an inbuilt function which is used to return the current working directory. This function does not accepts any parameter and returns the current working directory on successful function call or FALSE on failure. Syntax: getcwd()


2 Answers

chdir(__DIR__);

or

chdir(dirname(__FILE__));

(see chdir and magic constants).

But that should be by default.

like image 128
Czechnology Avatar answered Feb 01 '23 14:02

Czechnology


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__)); 
like image 27
Pekka Avatar answered Feb 01 '23 14:02

Pekka