(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)
Phar::addFile — 將一個(gè)文件從文件系統添加到 phar 檔案中
$filename, ?string $localName = null): void注意:
此方法需要 將 php.ini 中的
phar.readonly設為0以適合 Phar 對象. 否則, 將拋出PharException.
通過(guò)這個(gè)方法,任何文件或者 URL 可以被添加到 phar 檔案中。如果第二個(gè)可選參數 localName 被設定,
那么文件則會(huì )以該參數為名稱(chēng)保存到檔案中,此外,file 參數會(huì )被作為路徑保存在檔案中。
URLs 必須提交 localname 參數,否則會(huì )拋出異常。
這個(gè)方法與 ZipArchive::addFile() 類(lèi)似。
filename需要添加到 phar 檔案的文件在磁盤(pán)上的完全(絕對)或者相對路徑。
localName文件保存到檔案時(shí)的路徑。
沒(méi)有返回值,失敗時(shí)會(huì )拋出異常。
| 版本 | 說(shuō)明 |
|---|---|
| 8.0.0 |
localName 現在可以為空。
|
示例 #1 一個(gè) Phar::addFile() 示例
<?php
try {
$a = new Phar('/path/to/phar.phar');
$a->addFile('/full/path/to/file');
// demonstrates how this file is stored
$b = $a['full/path/to/file']->getContent();
$a->addFile('/full/path/to/file', 'my/file.txt');
$c = $a['my/file.txt']->getContent();
// demonstrate URL usage
$a->addFile('http://www.example.com', 'example.html');
} catch (Exception $e) {
// handle errors here
}
?>
注意: Phar::addFile(), Phar::addFromString() and Phar::offsetSet() save a new phar archive each time they are called. If performance is a concern, Phar::buildFromDirectory() or Phar::buildFromIterator() should be used instead.