In PHP how do you refer to files in an OS friendly manner? I’m looking at some code like
<?php
require_once(dirname(dirname(__FILE__)).'/common/config.inc.php');
...
that I have to run on a Windows machine, but it doesn’t parse the path right:
PHP Warning: require_once(C:\workspace/common/config.inc.php): failed to open stream: No such file or directory in C:\workspace\somescript.php on line 2
PHP Fatal error: require_once(): Failed opening required 'C:\workspace/common/config.inc.php' (include_path='.;C:\php5\pear') in C:\workspace\somescript.php on line 2
It looks like it’s trying to open with the forward-slashes which windows doesn’t like. The file C:\workspace\commonconfig.inc.php exists. The script is just not finding it because it has the forward-slashes right?
In the require_once statement, shouldn’t I be expressing the last part of the path in some os-friendly way? How do you do that?
In PHP, is there something similar to Python’s os.path.normpath(path) ? ..which takes a path-like string and returns the path appropriate to the running OS…
There are a few things you could use.
Instead of hardcoding the slashes, use the built-in constant
DIRECTORY_SEPARATOR, or as I prefer, make your own:..it makes your code a bit more compact.
Alternatively, use
realpath()and express all your paths with unix-style forward slashes, since: