Home > Article > Backend Development > How to convert path to actual path (absolute address) in php
In PHP, you can use the realpath() function to convert the path into an actual path, with the syntax "realpath(path)". The realpath() function can accept the relative path of a file as the value of the parameter "path", and then return the absolute path (information about the location of the actual file), which contains the file name.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php converts the path to the actual path (Absolute address) method
We know the relative path to execute the PHP script, assign it to $filename
, and then use realpath($filename
) to get the absolute path of the file (including the file name) and learn the location information of $filename
.
<?php $filename = "demo.php"; echo "{$filename} 文件所在位置: ".realpath($filename); ?>
The output result is:
It can be seen that of course the file $filename
is in the sub-section of the wamp directory on the c drive In the directory www.
The absolute path returned by the realpath() function contains the file name. If you don’t want it, you can use the dirname() function to remove it
<?php $filename = "demo.php"; echo "{$filename} 文件所在位置: ".dirname(realpath($filename)); ?>
Recommended learning: " PHP video tutorial》
The above is the detailed content of How to convert path to actual path (absolute address) in php. For more information, please follow other related articles on the PHP Chinese website!