In HTML, I can find a file starting from the web server's root folder by beginning the filepath with "/". Like:
/images/some_image.jpg
I can put that path in any file in any subdirectory, and it will point to the right image.
With PHP, I tried something similar:
include["/includes/header.php"];
...but that doesn't work.
I think that that this page is saying that I can
set include_path
once and after that, it will be assumed. But I don't quite get the syntax. Both examples start with a period, and it says:
Using a . in the include path allows for relative includes as it means the current directory.
Relative includes are exactly what I don't want.
How do I make sure that all my includes point to the root/includes
folder? [Bonus: what if I want to place that folder outside the public directory?]
Clarification
My development files are currently being served by XAMPP/Apache. Does that affect the absolute path? [I'm not sure yet what the production server will be.]
Update
I don't know what my problem was here. The include_path
thing I referenced above was exactly what I was looking for, and the syntax isn't really confusing. I just tried it and it works great.
One thing that occurs to me is that some people may have thought that "/some/path" was an "absolute path" because they assumed the OS was Linux. This server is Windows, so an absolute path would have to start with the drive name.
Anyway, problem solved! :]
[PHP 4, PHP 5, PHP 7, PHP 8]
realpath — Returns canonicalized absolute pathname
Description
realpath[string $path
]: string|false
Parameters
path
The path being checked.
Note:
Whilst a path must be supplied, the value can be an empty string. In this case, the value is interpreted as the current directory.
Return Values
Returns the canonicalized absolute pathname on success. The resulting path will have no symbolic link, /./
or /../
components. Trailing delimiters, such as \
and
/
, are also removed.
realpath[] returns false
on failure, e.g. if the file does not exist.
Note:
The running script must have executable permissions on all directories in the hierarchy, otherwise realpath[] will return
false
.
Note:
For case-insensitive filesystems realpath[] may or may not normalize the character case.
Note:
The function realpath[] will not work for a file which is inside a Phar as such path would be a virtual path, not a real one.
Note:
On Windows, junctions and symbolic links to directories are only expanded by one level.
Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.
Examples
Example #1 realpath[] example
The above example will output:
Example #2 realpath[] on Windows
On windows realpath[] will change unix style paths to windows style.
But you can fix it by clearing the realpath cache, this way :
imagiro ¶
11 years ago
Here is a small and handy method to calculate the relative path from $from to $to. Note: On Windows it does not work when $from and $to are on different drives.
eion at robbmob dot com ¶
6 years ago
Be aware that realpath[] doesn't work with hidden Windows UNC paths, eg \\servername\share$\folder\blah.txt but other PHP file-functions can access that file fine.
php at keith tyler dot com ¶
10 years ago
Note that under Windows, a slash-rooted path will resolve on the local drive, and *not* necessarily C:\.
For example:
M:\>php -r "print realpath['/AUTOEXEC.BAT'];"
[prints nothing, because there is no M:\AUTOEXEC.BAT]
But:
M:\>C:
C:\>php -r "print realpath['/AUTOEXEC.BAT'];"
C:\AUTOEXEC.BAT
Same script, different response depending on current drive.
I'm inclined to argue that this function *should* use the value of %SystemDrive% as the "slash root" base.
Lars Scheithauer ¶
17 years ago
This function is also nice to test for security-breaches. You can forbid the script to access files below a certain directory to prevent "../../../etc/shadow" and similar attacks:
The url "script.php?file_to_get=../../../etc/shadow" will now result in an error.
Leonard Challis ¶
9 years ago
When using realpath [and similar functions] remember that PHP will take in to account open_basedir restrictions. So, if you do something like:
where your open_basedir setting is set to the httpdocs folder and tmp, this will return false. You must set it to the level above [or off] for this to work.
Enzo dot Barbaguelatta at alma dot cl ¶
6 months ago
Be noticed that, one of the reasons of realtpath retuning false can be if the path does not exist, or it has permission issues, or even if security modules of your operating system [let's say, SELinux for example] avoids you checking the path.
If you're like me and you had a not very good time checking why realpath retuns false, please check these points first.