PHP – Include relative path

If the PHP script is executed from a shell script that isn’t residing in the same directory, the php include statement would fail as it would look for the include file within the directory where the shell script is executed from. Therefore, the usage of __DIR__ in the include statement is much more desired way of including files.

include_once __DIR__."/../dbConn.php";

Note that the __DIR__ path doesn’t include the trailing slash so you may want to append it in your own code if you’re gathering the file from a different location.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s