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

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

    以字符串的形式添加一个文件到 phar 档案

    说明

    publicPhar::addFromString(string $localname,string $contents): void
    Note:

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

    通过这个方法,任何字符串都可以被添加到 phar 档案中。文件将会以localname为路径保存到档案中。这个方法与ZipArchive::addFromString()类似。

    参数

    $localname

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

    $contents

    要保存的文件内容。

    返回值

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

    范例

    一个Phar::addFromString()示例

    <?php
    try {
        $a = new Phar('/path/to/phar.phar');
        $a->addFromString('path/to/file.txt', 'my simple file');
        $b = $a['path/to/file.txt']->getContent();
        // to add contents from a stream handle for large files, use offsetSet()
        $c = fopen('/path/to/hugefile.bin');
        $a['largefile.bin'] = $c;
        fclose($c);
    } catch (Exception $e) {
        // handle errors here
    }
    ?>
    

    参见

    • Phar::offsetSet() Set the contents of an internal file to those of an external file
    • PharData::addFromString() Add a file from the filesystem to the tar/zip archive
    • Phar::addFile() 将一个文件从文件系统添加到 phar 档案中
    • Phar::addEmptyDir() 添加一个空目录到 phar 档案