PHP8 Phar::addFromString

2024-02-20 09:16 更新

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

Phar::addFromString — 以字符串的形式添加一個文件到 phar 檔案

說明

public Phar::addFromString(string $localName, string $contents): void
注意:此方法需要 將 php.ini 中的 phar.readonly 設(shè)為 0 以適合 Phar 對象. 否則, 將拋出PharException.

通過這個方法,任何字符串都可以被添加到 phar 檔案中。 文件將會以 localname 為路徑保存到檔案中。 這個方法與 ZipArchive::addFromString() 類似。

參數(shù)

localName

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

contents

要保存的文件內(nèi)容。

返回值

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

示例

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

參見

  • Phar::offsetSet() - Set the contents of an internal file to those of an external file
  • PharData::addFromString() - Add a file from a string to the tar/zip archive
  • Phar::addFile() - 將一個文件從文件系統(tǒng)添加到 phar 檔案中
  • Phar::addEmptyDir() - 添加一個空目錄到 phar 檔案


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號