PHP8 Phar::webPhar

2024-02-21 14:40 更新

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

Phar::webPhar — 將來自 Web 瀏覽器的請求路由到 phar 存檔中的內(nèi)部文件

說明

final public static Phar::webPhar(
    ?string $alias = null,
    ?string $index = null,
    ?string $fileNotFoundScript = null,
    array $mimeTypes = [],
    ?callable $rewrite = null
): void

Phar::webPhar() 用作 Phar::mapPhar() 基于 Web 的 Phars。此方法解析和 將來自 Web 瀏覽器的請求路由到 Phar 存檔中的內(nèi)部文件。 它模擬 Web 服務器,將請求路由到正確的文件,并回顯正確的文件 標頭并根據(jù)需要解析 PHP 文件。結(jié)合 Phar::mungServer() 和 Phar::interceptFileFuncs(),可以使用任何 Web 應用程序 未從 Phar 存檔中修改。$_SERVER['REQUEST_URI']

Phar::webPhar() 應該只 從 Phar 存檔的存根調(diào)用(有關什么是存根的更多信息,請參閱此處)。

參數(shù) 

alias

可在 URL 中使用的別名 請參閱此存檔,而不是其完整路徑。phar://

index

目錄索引的 phar 中的位置。

fileNotFoundScript

找不到文件時要運行的腳本的位置。這 腳本應輸出正確的 HTTP 404 標頭。

mimeTypes

將其他文件擴展名映射到 MIME 類型的數(shù)組。 如果默認映射足夠,則傳遞一個空數(shù)組。 默認情況下,這些擴展映射到以下 MIME 類型:

<?php
$mimes = array(
    'phps' => Phar::PHPS, // pass to highlight_file()
    'c' => 'text/plain',
    'cc' => 'text/plain',
    'cpp' => 'text/plain',
    'c++' => 'text/plain',
    'dtd' => 'text/plain',
    'h' => 'text/plain',
    'log' => 'text/plain',
    'rng' => 'text/plain',
    'txt' => 'text/plain',
    'xsd' => 'text/plain',
    'php' => Phar::PHP, // parse as PHP
    'inc' => Phar::PHP, // parse as PHP
    'avi' => 'video/avi',
    'bmp' => 'image/bmp',
    'css' => 'text/css',
    'gif' => 'image/gif',
    'htm' => 'text/html',
    'html' => 'text/html',
    'htmls' => 'text/html',
    'ico' => 'image/x-ico',
    'jpe' => 'image/jpeg',
    'jpg' => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'js' => 'application/x-javascript',
    'midi' => 'audio/midi',
    'mid' => 'audio/midi',
    'mod' => 'audio/mod',
    'mov' => 'movie/quicktime',
    'mp3' => 'audio/mp3',
    'mpg' => 'video/mpeg',
    'mpeg' => 'video/mpeg',
    'pdf' => 'application/pdf',
    'png' => 'image/png',
    'swf' => 'application/shockwave-flash',
    'tif' => 'image/tiff',
    'tiff' => 'image/tiff',
    'wav' => 'audio/wav',
    'xbm' => 'image/xbm',
    'xml' => 'text/xml',
);
?>
rewrite

rewrites 函數(shù)將字符串作為其唯一參數(shù)傳遞,并且必須返回字符串或 false。

如果您使用的是 fast-cgi 或 cgi,則傳遞給函數(shù)的參數(shù)是變量的值。否則,傳遞給函數(shù)的參數(shù)是值 的變量。$_SERVER['PATH_INFO']$_SERVER['REQUEST_URI']

如果返回字符串,則將其用作內(nèi)部文件路徑。如果返回 false,則 webPhar() 將 發(fā)送 HTTP 403 拒絕代碼。

返回值 

沒有返回值。

錯誤/異常 

無法打開內(nèi)部時引發(fā) PharException 文件輸出,或者如果 從非存根調(diào)用。如果傳遞了無效的數(shù)組值或傳遞了無效的回調(diào),則會引發(fā) UnexpectedValueException。mimeTypesrewrite

更新日志 

版本說明
8.0.0fileNotFoundScript并且現(xiàn)在是可空的。rewrite

示例 

示例 #1 A Phar::webPhar() example

在下面的示例中,創(chuàng)建的 phar 將顯示 if one browses to 或 to ,并將顯示 if one browses to 的來源。Hello World/myphar.phar/index.php/myphar.pharindex.phps/myphar.phar/index.phps

<?php
// creating the phar archive:
try {
    $phar = new Phar('myphar.phar');
    $phar['index.php'] = '<?php echo "Hello World"; ?>';
    $phar['index.phps'] = '<?php echo "Hello World"; ?>';
    $phar->setStub('<?php
Phar::webPhar();
__HALT_COMPILER(); ?>');
} catch (Exception $e) {
    // handle error here
}
?>

參見 

  • Phar::mungServer() - 定義最多 4 個 $_SERVER 變量的列表,這些變量應修改以執(zhí)行
  • Phar::interceptFileFuncs() - 指示 phar 攔截 fopen、file_get_contents、opendir 和所有與 stat 相關的函數(shù)


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號