(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)
PharData::addFromString — 將字串中的檔案新增至 tar/zip 封存
使用此方法,可以將任何字串新增至 tar/zip 封存。 檔案將以 localname
作為其路徑儲存在封存中。 此方法類似於 ZipArchive::addFromString()。
localName
檔案將儲存在封存中的路徑。
contents
要儲存的檔案內容
無回傳值,失敗時拋出例外。
範例 #1 PharData::addFromString() 範例
<?php
try {
$a = new PharData('/path/to/my.tar');
$a->addFromString('path/to/file.txt', 'my simple file');
$b = $a['path/to/file.txt']->getContent();
// 對於大型檔案,要從串流控制代碼新增內容,請使用 offsetSet()
$c = fopen('/path/to/hugefile.bin');
$a['largefile.bin'] = $c;
fclose($c);
} catch (Exception $e) {
// 在此處處理錯誤
}
?>
注意:每次呼叫 PharData::addFile()、PharData::addFromString() 和 PharData::offsetSet() 時,都會儲存一個新的 phar 封存檔。如果擔心效能問題,則應改用 PharData::buildFromDirectory() 或 PharData::buildFromIterator()。