Phar::addFile

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

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

說(shuō)明

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

注意:

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

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

參數(shù)

filename

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

localName

文件保存到檔案時(shí)的路徑。

返回值

沒有返回值,失敗時(shí)會(huì)拋出異常。

更新日志

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

范例

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

參見