导航首页 » 技术教程 » PHP:realpath()的用法_Filesystem函数
PHP:realpath()的用法_Filesystem函数 147 2023-12-15   

realpath

(PHP 4, PHP 5)

realpath — 返回规范化的绝对路径名

说明

string realpath ( string $path )

realpath() 扩展所有的符号连接并且处理输入的 path 中的 '/./', '/../' 以及多余的 '/' 并返回规范化后的绝对路径名。返回的路径中没有符号连接,'/./' 或 '/../' 成分。

参数

path

要检查的路径。

Note:

Whilst a path must be supplied, the value can be blank or NULL In these cases, the value is interpreted as the current directory.

返回值

Returns the canonicalized absolute pathname on success. The resulting path will have no symbolic link, '/./' or '/../' components.

realpath() 失败时返回 FALSE,比如说文件不存在的话。

Note:

The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE.

Note: 因为 PHP 的整数类型是有符号整型而且很多平台使用 32 位整型,对 2GB 以上的文件,一些文件系统函数可能返回无法预期的结果 。

更新日志

版本 说明 5.3.0 在之前的版本中,在 *BSD 系统上,如果仅仅是 path 不存在的话,realpath() 并不会像其它系统那样返回 FALSE。 5.0.0 在此之前的版本中,如果 path 传入了空或者 NULL,将导致 realpath() 返回脚本当前的目录。

范例

Example #1 realpath() 例子

<?php
chdir('/var/www/');
echo realpath('./../../etc/passwd');
?>

以上例程会输出:

/etc/passwd

Example #2 Windows 上的 realpath()

在 Windows 上,realpath() 会将 unix 风格的路径改成 Windows 风格的。

<?php
echo realpath('/windows/system32');
?>

以上例程会输出:

C:WINDOWSSystem32

参见

basename() - 返回路径中的文件名部分 dirname() - 返回路径中的目录部分 pathinfo() - 返回文件路径的信息


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103