Phar::addFile

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

Phar::addFile将一个文件从文件系统添加到 phar 档案中

说明

public Phar::addFile ( string $file [, string $localname ] ) : void

Note:

此方法需要 将 php.ini 中的 phar.readonly 设为 0 以适合 Phar 对象. 否则, 将抛出PharException.

通过这个方法,任何文件或者 URL 可以被添加到 phar 档案中。如果第二个可选参数 localname 被设定, 那么文件则会以该参数为名称保存到档案中,此外,file 参数会被作为路径保存在档案中。 URLs 必须提交 localname 参数,否则会抛出异常。 这个方法与 ZipArchive::addFile() 类似。

参数

file

需要添加到 phar 档案的文件在磁盘上的完全(绝对)或者相对路径。

localname

文件保存到档案时的路径。

返回值

没有返回值,失败时会抛出异常。

范例

Example #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
}
?>

参见