查詢

Phar::addFromString()函式—用法及示例

「 將字串內容新增到Phar存檔中的指定檔案 」


函式名:Phar::addFromString()

適用版本:PHP 5 >= 5.3.0, PHP 7, PHP 8

函式描述:將字串內容新增到Phar存檔中的指定檔案。

語法:bool Phar::addFromString(string $file, string $content)

引數:

  • $file:要新增內容的檔名,可以是相對路徑或絕對路徑。
  • $content:要新增的字串內容。

返回值:

  • 成功時返回 true,失敗時返回 false。

示例:

<?php
$phar = new Phar('myphar.phar');

// 新增字串內容到檔案
$phar->addFromString('file.txt', 'This is the content of the file.');

// 檢查檔案是否新增成功
if ($phar->offsetExists('file.txt')) {
    echo 'File added successfully.';
} else {
    echo 'Failed to add file.';
}

// 讀取檔案內容
$fileContent = $phar->getContents('file.txt');
echo $fileContent;
?>

在上面的示例中,我們建立了一個名為myphar.phar的Phar存檔物件。然後,使用addFromString()函式將字串內容This is the content of the file.新增到名為file.txt的檔案中。最後,我們透過offsetExists()函式檢查檔案是否成功新增,並使用getContents()函式讀取檔案內容並輸出。

請注意,為了使用addFromString()函式,您需要啟用Phar擴充套件。

補充糾錯
上一個函式: Phar::apiVersion()函式
下一個函式: Phar::addFile()函式
熱門PHP函式
分享連結