PHP8 使用 Phar Archives簡(jiǎn)介

2024-02-19 15:45 更新

Phar 歸檔在概念上類似于 Java JAR 歸檔,但都是定制的 滿足 PHP 應(yīng)用程序的需求和靈活性。Phar 檔案是 用于在單個(gè)文件中分發(fā)完整的 PHP 應(yīng)用程序或庫。一個(gè) Phar 存檔應(yīng)用程序的使用方式與任何其他 PHP 應(yīng)用程序完全相同:

php coolapplication.phar
  

使用 Phar 存檔庫與使用任何其他 PHP 庫相同:

<?php
include 'coollibrary.phar';
?>

流包裝器提供了 phar 擴(kuò)展的核心,并且 這里 詳細(xì)解釋。 phar 流包裝器允許使用 PHP 的標(biāo)準(zhǔn)文件函數(shù) fopen()、opendir() 和 其他處理常規(guī)文件的。流包裝器支持所有 對(duì)文件和目錄的讀/寫操作。pharphar

<?php
include 'phar://coollibrary.phar/internal/file.php';
header('Content-type: image/jpeg');
// phars can be accessed by full path or by alias
echo file_get_contents('phar:///fullpath/to/coollibrary.phar/images/wow.jpg');
?>

Phar 類實(shí)現(xiàn)了用于訪問的高級(jí)功能 文件和用于創(chuàng)建 PHAR 檔案。這里詳細(xì)解釋了 Phar 類。

<?php
try {
// open an existing phar
$p = new Phar('coollibrary.phar', 0);
// Phar extends SPL's DirectoryIterator class
foreach (new RecursiveIteratorIterator($p) as $file) {
// $file is a PharFileInfo class, and inherits from SplFileInfo
echo $file->getFileName() . "\n";
echo file_get_contents($file->getPathName()) . "\n"; // display contents;
}
if (isset($p['internal/file.php'])) {
var_dump($p['internal/file.php']->getMetadata());
}

// create a new phar - phar.readonly must be 0 in php.ini
// phar.readonly is enabled by default for security reasons.
// On production servers, Phars need never be created,
// only executed.
if (Phar::canWrite()) {
$p = new Phar('newphar.tar.phar', 0, 'newphar.tar.phar');
// make this a tar-based phar archive, compressed with gzip compression (.tar.gz)
$p = $p->convertToExecutable(Phar::TAR, Phar::GZ);

// create transaction - nothing is written to newphar.phar
// until stopBuffering() is called, although temporary storage is needed
$p->startBuffering();
// add all files in /path/to/project, saving in the phar with the prefix "project"
$p->buildFromIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/project')), '/path/to/');

// add a new file via the array access API
$p['file1.txt'] = 'Information';
$fp = fopen('hugefile.dat', 'rb');
// copy all data from the stream
$p['data/hugefile.dat'] = $fp;

if (Phar::canCompress(Phar::GZ)) {
$p['data/hugefile.dat']->compress(Phar::GZ);
}

$p['images/wow.jpg'] = file_get_contents('images/wow.jpg');
// any value can be saved as file-specific meta-data
$p['images/wow.jpg']->setMetadata(array('mime-type' => 'image/jpeg'));
$p['index.php'] = file_get_contents('index.php');
$p->setMetadata(array('bootstrap' => 'index.php'));

// save the phar archive to disk
$p->stopBuffering();
}
} catch (Exception $e) {
echo 'Could not open Phar: ', $e;
}
?>

此外,可以使用任何 支持的對(duì)稱哈希算法(MD5、SHA1、SHA256 和 SHA512,如果啟用了 ext/hash) 以及使用 OpenSSL 使用非對(duì)稱公鑰/私鑰簽名。自 利用 OpenSSL 簽名,您需要生成公鑰/私鑰對(duì),并且 使用私鑰通過 Phar::setSignatureAlgorithm() 設(shè)置簽名。此外,公鑰 使用以下代碼提?。?/p>

<?php
$public = openssl_get_publickey(file_get_contents('private.pem'));
$pkey = '';
openssl_pkey_export($public, $pkey);
?>

必須保存在它驗(yàn)證的 Phar 存檔旁邊。如果 phar 存檔 保存為 ,必須保存公鑰 as ,否則 phar 將無法驗(yàn)證 OpenSSL 簽名。

/path/to/my.phar/path/to/my.phar.pubkey

Phar 類還提供了 3 個(gè)靜態(tài)方法,Phar::webPhar()、Phar::mungServer() 和 Phar: :interceptFileFuncs(),它們至關(guān)重要 打包專為在常規(guī)文件系統(tǒng)和基于 Web 的應(yīng)用程序上使用而設(shè)計(jì)的 PHP 應(yīng)用程序。Phar::webPhar() 實(shí)現(xiàn)了一個(gè)前端控制器,該控制器將 HTTP 調(diào)用路由到正確的 在 Phar 檔案中的位置。Phar::mungServer() 用于修改 用于欺騙處理這些值的應(yīng)用程序的數(shù)組。Phar::interceptFileFuncs() 指示 Phar 攔截對(duì) fopen()、file_get_contents()、opendir() 和  所有基于統(tǒng)計(jì)的函數(shù)(file_exists()、is_readable() 等)和 將所有相對(duì)路徑路由到 Phar 存檔中的位置。$_SERVER

例如,打包流行的 phpMyAdmin 應(yīng)用程序的發(fā)行版以用作 phar 存檔需要 只有這個(gè)簡(jiǎn)單的腳本,然后可以作為常規(guī)文件訪問 修改用戶/密碼后從您的 Web 服務(wù)器:phpMyAdmin.phar.tar.php

<?php
@unlink('phpMyAdmin.phar.tar.php');
copy('phpMyAdmin-2.11.3-english.tar.gz', 'phpMyAdmin.phar.tar.php');
$a = new Phar('phpMyAdmin.phar.tar.php');
$a->startBuffering();
$a["phpMyAdmin-2.11.3-english/config.inc.php"] = '<?php
/* Servers configuration */
$i = 0;

/* Server localhost (config:root) [1] */
$i++;
$cfg[\'Servers\'][$i][\'host\'] = \'localhost\';
$cfg[\'Servers\'][$i][\'extension\'] = \'mysqli\';
$cfg[\'Servers\'][$i][\'connect_type\'] = \'tcp\';
$cfg[\'Servers\'][$i][\'compress\'] = false;
$cfg[\'Servers\'][$i][\'auth_type\'] = \'config\';
$cfg[\'Servers\'][$i][\'user\'] = \'root\';
$cfg[\'Servers\'][$i][\'password\'] = \'\';


/* End of servers configuration */
if (strpos(PHP_OS, \'WIN\') !== false) {
$cfg[\'UploadDir\'] = getcwd();
} else {
$cfg[\'UploadDir\'] = \'/tmp/pharphpmyadmin\';
@mkdir(\'/tmp/pharphpmyadmin\');
@chmod(\'/tmp/pharphpmyadmin\', 0777);
}';
$a->setStub('<?php
Phar::interceptFileFuncs();
Phar::webPhar("phpMyAdmin.phar", "phpMyAdmin-2.11.3-english/index.php");
echo "phpMyAdmin is intended to be executed from a web browser\n";
exit -1;
__HALT_COMPILER();
');
$a->stopBuffering();
?>


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)