Phar::addFile

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

Phar::addFile將一個文件從文件系統(tǒng)添加到 phar 檔案中

說明

public Phar::addFile(string $filename, ?string $localName = null): void

注意:

此方法需要 將 php.ini 中的 phar.readonly 設(shè)為 0 以適合 Phar 對象. 否則, 將拋出PharException.

通過這個方法,任何文件或者 URL 可以被添加到 phar 檔案中。如果第二個可選參數(shù) localName 被設(shè)定, 那么文件則會以該參數(shù)為名稱保存到檔案中,此外,file 參數(shù)會被作為路徑保存在檔案中。 URLs 必須提交 localname 參數(shù),否則會拋出異常。 這個方法與 ZipArchive::addFile() 類似。

參數(shù)

filename

需要添加到 phar 檔案的文件在磁盤上的完全(絕對)或者相對路徑。

localName

文件保存到檔案時的路徑。

返回值

沒有返回值,失敗時會拋出異常。

更新日志

版本 說明
8.0.0 localName 現(xiàn)在可以為空。

范例

示例 #1 一個 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.

參見