• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • Phar::addFile()

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

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

    说明

    publicPhar::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

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

    返回值

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

    范例

    一个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::offsetSet() Set the contents of an internal file to those of an external file
    • PharData::addFile() Add a file from the filesystem to the tar/zip archive
    • Phar::addFromString() 以字符串的形式添加一个文件到 phar 档案
    • Phar::addEmptyDir() 添加一个空目录到 phar 档案