2024 年日本 PHP 研討會

SplFileInfo::openFile

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

SplFileInfo::openFile取得檔案的 SplFileObject 物件

說明

公開 SplFileInfo::openFile(字串 $mode = "r", 布林值 $useIncludePath = false, ?資源 $context = null): SplFileObject

建立一個檔案的 SplFileObject 物件。這很有用,因為 SplFileObject 包含操作檔案的額外方法,而 SplFileInfo 僅用於獲取資訊,例如檔案是否可寫入。

參數

mode

開啟檔案的模式。有關可能模式的說明,請參閱 fopen() 文件。預設值是唯讀。

useIncludePath

當設定為 true 時,也會在 include_path 中搜尋檔名。

context

有關 contexts 的說明,請參閱手冊的 context 章節。

回傳值

已開啟的檔案,作為 SplFileObject 物件

錯誤/例外

如果無法開啟檔案(例如,存取權限不足),則會產生 RuntimeException

更新日誌

版本 說明
8.0.0 context 現在可以為 null。

範例

範例 #1 SplFileInfo::openFile() 範例

<?php
$fileinfo
= new SplFileInfo('/tmp/foo.txt');

if (
$fileinfo->isWritable()) {

$fileobj = $fileinfo->openFile('a');

$fileobj->fwrite("appended this sample text");
}
?>

參見

新增註釋

使用者貢獻的註釋

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