Phar::webPhar

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

Phar::webPharRoutes a request from a web browser to an internal file within the phar archive

Description

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

Phar::webPhar() serves as Phar::mapPhar() for web-based phars. This method parses $_SERVER['REQUEST_URI'] and routes a request from a web browser to an internal file within the phar archive. It simulates a web server, routing requests to the correct file, echoing the correct headers and parsing PHP files as needed. Combined with Phar::mungServer() and Phar::interceptFileFuncs(), any web application can be used unmodified from a phar archive.

Phar::webPhar() should only be called from the stub of a phar archive (see here for more information on what a stub is).

Parameters

alias

The alias that can be used in phar:// URLs to refer to this archive, rather than its full path.

index

The location within the phar of the directory index.

fileNotFoundScript

The location of the script to run when a file is not found. This script should output the proper HTTP 404 headers.

mimeTypes

An array mapping additional file extensions to MIME type. If the default mapping is sufficient, pass an empty array. By default, these extensions are mapped to these MIME types:

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

The rewrites function is passed a string as its only parameter and must return a string or false.

If you are using fast-cgi or cgi then the parameter passed to the function is the value of the $_SERVER['PATH_INFO'] variable. Otherwise, the parameter passed to the function is the value of the $_SERVER['REQUEST_URI'] variable.

If a string is returned it is used as the internal file path. If false is returned then webPhar() will send a HTTP 403 Denied Code.

Return Values

No value is returned.

Errors/Exceptions

Throws PharException when unable to open the internal file to output, or if called from a non-stub. If an invalid array value is passed into mimeTypes or an invalid callback is passed into rewrite, then UnexpectedValueException is thrown.

Changelog

Version Description
8.0.0 fileNotFoundScript, mimeTypes and rewrite are nullable now.

Examples

Example #1 A Phar::webPhar() example

With the example below, the created phar will display Hello World if one browses to /myphar.phar/index.php or to /myphar.phar, and will display the source of index.phps if one browses to /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
}
?>

See Also

  • Phar::mungServer() - Defines a list of up to 4 $_SERVER variables that should be modified for execution
  • Phar::interceptFileFuncs() - Instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial