PHP Conference Japan 2024

Phar::interceptFileFuncs

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)

Phar::interceptFileFuncs指示 phar 攔截 fopen、file_get_contents、opendir 以及所有 stat 相關函式

說明

final public static Phar::interceptFileFuncs(): void

指示 phar 攔截 fopen()readfile()file_get_contents()opendir() 以及所有 stat 相關函式。如果在 phar 封存中使用相對路徑呼叫這些函式中的任何一個,則會修改呼叫以存取 phar 封存中的檔案。絕對路徑則會被視為嘗試從檔案系統載入外部檔案。

此函數讓設計用於硬碟執行的 PHP 應用程式能夠以 phar 應用程式的方式執行。

參數

無參數。

傳回值

範例

範例 #1 Phar::interceptFileFuncs() 範例

<?php
Phar
::interceptFileFuncs();
include
'phar://' . __FILE__ . '/file.php';
?>

假設此 phar 位於 /path/to/myphar.phar 並包含 file.phpfile2.txt,如果 file.php 包含以下程式碼

範例 #2 Phar::interceptFileFuncs() 範例

<?php
echo file_get_contents('file2.txt');
?>

一般情況下,PHP 會在目前的目錄中搜尋 file2.txt,這會被轉譯為 file.php 的目錄,或是命令列使用者的目前目錄。 Phar::interceptFileFuncs() 指示 PHP 將目前目錄視為 phar:///path/to/myphar.phar/,因此在上述範例程式碼中會開啟 phar:///path/to/myphar.phar/file2.txt

新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top