Phar::addFromString

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

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

說明

public Phar::addFromString(string $localname, string $contents): void

注意:

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

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

參數(shù)

localname

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

contents

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

返回值

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

范例

示例 #1 一個(gè) 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
}
?>

參見