Shlomo
Shlomo

Reputation: 3990

PHP require realpath - Failed opening required

I am trying to require/include a file by relative path. The paths exist and all rights are checked. The script to be included is readable and can be accessed via URL in browser...

However, I am receiving errors/warnings that it failed to open. Both the calling script and the file to be included are on the same server.

Caller relative:

/devel/phi/dev/appcenter-head/appcenter/application/nm/index.php

Caller location:

/home/devel/wwwroot/phi/dev/appcenter-head/appcenter/application/nm

Require relative:

/devel/nm/dev/http-api/http-api/src/httpapi.inc.php

Require location:

/home/devel/wwwroot/nm/dev/http-api/http-api/src

What I tried:

$strHttpApiUrl = "/devel/nm/dev/http-api/http-api/index.php";
$strFile = "/src/httpapi.inc.php";
$strDirName = dirname( $strHttpApiUrl );
$strRealpath = realpath( $strDirName . $strFile );
require_once $strRealpath;

Fails with: PHP Warning: require_once(/home/devel/wwwroot/phi/dev/appcenter-head/appcenter/application/nm) [<a href='function.require-once'>functio n.require-once</a>]: failed to open stream: No such file or directory in /home/devel/wwwroot/phi/dev/appcenter-head/appcenter/application/nm/class/TWinLogin.php

Also fails with: PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required '' (include_path='.:/opt/nm/appcenter40-server-php53/lib/php')

Also tried plain including the string URL or using absolute path...

How can I include that file?


EDIT:

This successfully includes the file:

require_once '/home/devel/wwwroot/nm/dev/http-api/http-api/src/httpapi.inc.php';

So how can I get that working path, assume I get the following relative path:

/devel/nm/dev/http-api/http-api/index.php

Upvotes: 2

Views: 1394

Answers (1)

gaRex
gaRex

Reputation: 4215

realpath tries to get real path in file system -- absolute path. So it most possibly starts with slash.

If your resulting path starts with slash and php tries to require/include it, most possibly it will think that its' absolute. And if it not see there existing file -- it gives you those error.

If you want to use relative pathes, avoid realpath.

Upvotes: 2

Related Questions