stream_resolve_include_path() 似乎會快取其輸出。在我重新命名一個檔案後,我必須重新啟動 Apache 才能讓 stream_resolve_include_path() 不返回不存在的檔案名稱。這是在 Windows 上發生的。
(PHP 5 >= 5.3.2, PHP 7, PHP 8)
stream_resolve_include_path — 依 include path 解析檔名
filename
要解析的檔名。
範例 #1 stream_resolve_include_path() 範例
基本用法範例。
<?php
var_dump(stream_resolve_include_path("test.php"));
?>
以上範例將輸出類似以下的內容:
string(22) "/var/www/html/test.php"
stream_resolve_include_path() 似乎會快取其輸出。在我重新命名一個檔案後,我必須重新啟動 Apache 才能讓 stream_resolve_include_path() 不返回不存在的檔案名稱。這是在 Windows 上發生的。
在某些情況下,例如這樣,您不能在沒有解析路徑的情況下使用 `realpath()` 或 `file_exists()`。
範例
file.php
subfolder/
..|- included.php
..|- subfolder/
.........|- another-included.php
file.php 內容
```
<?php
var_dump(file_exists('subfolder/included.php'));// true
include 'subfolder/included.php';
?>
```
subfolder/included.php 內容
```
<?php
var_dump(file_exists('subfolder/another-included.php'));// false,但檔案實際上存在。
var_dump(file_exists(stream_resolve_include_path('subfolder/another-included.php')));// 使用 `stream_resolve_include_path()` 函式後,現在會返回 true。
include 'subfolder/another-included.php';// 正常運作,沒有錯誤。
?>
```
subfolder/subfolder/another-included.php 內容
```
<?php
echo 'Hello world';
?>
```