PHP8 PharData::addFile

2024-02-22 11:48 更新

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

PharData::addFile — 將文件系統(tǒng)中的文件添加到 tar/zip 存檔中

說明

public PharData::addFile(string $filename, ?string $localName = null): void

使用此方法,可以將任何文件或 URL 添加到 tar/zip 存檔中。如果 指定了可選的第二個(gè)參數(shù), 該文件將以該名稱存儲(chǔ)在存檔中,否則該參數(shù)將用作存儲(chǔ)在 存檔。URL 必須具有 localname,否則會(huì)引發(fā)異常。 此方法類似于 ZipArchive::addFile()。localnamefile

參數(shù) 

filename

要添加的磁盤上文件的完整路徑或相對路徑 到Phar檔案館。

localName

文件將存儲(chǔ)在存檔中的路徑。

返回值 

無返回值,失敗時(shí)引發(fā)異常。

更新日志 

版本說明
8.0.0localName現(xiàn)在是可為 null 的。

示例 

示例 #1 A PharData::addFile() example

<?php
try {
    $a = new PharData('/path/to/my.tar');

    $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
}
?>

注釋 

注意: PharData::addFile(), PharData:: addFromString() 和 PharData::offsetSet() 每次調(diào)用它們時(shí)都會(huì)保存一個(gè)新的 phar 存檔。如果性能是一個(gè)問題,則應(yīng)改用 PharData::buildFromDirectory() 或 PharData::buildFromIterator()。

參見 

  • PharData::offsetSet() - 將 tar/zip 中文件的內(nèi)容設(shè)置為外部文件或字符串的內(nèi)容
  • Phar::addFile() - 將一個(gè)文件從文件系統(tǒng)添加到 phar 檔案中
  • PharData::addFromString() - 將字符串中的文件添加到 tar/zip 存檔中
  • PharData::addEmptyDir() - 在 tar/zip 存檔中添加一個(gè)空目錄


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號