ConFoo 2025

SplFileInfo::getRealPath

(PHP 5 >= 5.2.2, PHP 7, PHP 8)

SplFileInfo::getRealPathGets absolute path to file

说明

public SplFileInfo::getRealPath(): string|false

This method expands all symbolic links, resolves relative references and returns the real path to the file.

参数

此函数没有参数。

返回值

Returns the path to the file, or false if the file does not exist.

示例

示例 #1 SplFileInfo::getRealPath() example

<?php
$info
= new SplFileInfo('/..//./../../'.__FILE__);
var_dump($info->getRealPath());

$info = new SplFileInfo('/tmp');
var_dump($info->getRealPath());

$info = new SplFileInfo('/I/Do/Not/Exist');
var_dump($info->getRealPath());

$info = new SplFileInfo('php://output');
var_dump($info->getRealPath());

$info = new SplFileInfo("");
var_dump($info->getRealPath());
?>

以上示例的输出类似于:

string(28) "/private/tmp/phptempfile.php" 
string(12) "/private/tmp"
bool(false)
bool(false)
string(12) "/private/tmp"

参见

添加备注

用户贡献的备注 1 note

up
3
giel at berkers dot online
6 years ago
Note that due to realpath caching this method will return false when the file has been renamed or unlinked by other processes.

Example:

<?php
$info
= new SplFileInfo('example.php');
$info->getRealPath(); // /path/to/example.php

rename('example.php', 'foobar.php');

$info->getRealPath(); // false
?>
To Top