TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

PHP Relative paths like ASP

This works

<?php include("inc/c.php")?>

But in a folder past this, this does not work

<?php include("../inc/c.php")?>

I have to do

<?php include("/var/web/public_html/etc/inc/c.php")?>

I know in ASP you can enable virtual paths and directories. Is this the same with PHP?

Upvotes: 0

Views: 146

Answers (5)

Your Common Sense
Your Common Sense

Reputation: 157896

But in a folder past this, this does not work

Nope, it does work.

I know in ASP you can enable virtual paths and directories. Is this the same with PHP?

Yes. But whole virtual path thing has nothing to do with your case.

This is not PHP problem. This is developer's problem who is using wrong path.

To make your code fool-proof, always use absolute paths. Build paths not from current location but from the site root. So, it will be all the same in the ANY page on your site.
Most general way would be

include $_SERVER['DOCUMENT_ROOT']."/etc/inc/c.php";

Upvotes: -2

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

You can use realpath(dirname(__FILE__)) to include files relatively to current file:

include(realpath(dirname(__FILE__).'/../inc/c.php')); 

Upvotes: 1

CoreCoder
CoreCoder

Reputation: 419

use

dirname(__file__)

it will return you path of current directory.

Upvotes: 0

Martin Bean
Martin Bean

Reputation: 39389

You can add directories to PHP's include_path directory. When you specify a relative file name, PHP will look for that file relative to all directories specified in the include_path.

Take a look at http://php.net/manual/en/function.set-include-path.php#example-488.

Upvotes: 0

user142162
user142162

Reputation:

If you're including a file from a folder, all includes are relative to the includer's file.

Therefore, the same code should work for the file in the sub-folder:

<?php include("inc/c.php")?>

Upvotes: 2

Related Questions