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

Beschreibung

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).

Parameter-Liste

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.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Fehler/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 Beschreibung
8.0.0 fileNotFoundScript, mimeTypes and rewrite are nullable now.

Beispiele

Beispiel #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
}
?>

Siehe auch

  • 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

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

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

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

[Wichtig] Anmeldung im Forum

Guess the hidden word using logic and deduction in the engaging game of Wordle. Wordle nyt (https://wordlenytimes.net/) ​

Geschrieben von lenytimes am 19.04.2024 19:21:45
Forum: Fragen/Vorschläge zum Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum