Home  >  Article  >  Backend Development  >  How to convert path to actual path (absolute address) in php

How to convert path to actual path (absolute address) in php

青灯夜游
青灯夜游Original
2022-06-10 18:46:112655browse

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.

How to convert path to actual path (absolute address) in php

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:

How to convert path to actual path (absolute address) in php

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));
?>

How to convert path to actual path (absolute address) in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn