Using Phar Archives: the Phar and PharData class

The Phar class supports reading and manipulation of Phar archives, as well as iteration through inherited functionality of the RecursiveDirectoryIterator class. With support for the ArrayAccess interface, files inside a Phar archive can be accessed as if they were part of an associative array.

The PharData class extends the Phar, and allows creating and modifying non-executable (data) tar and zip archives even if phar.readonly=1 in php.ini. As such, PharData::setAlias() and PharData::setStub() are both disabled as the concept of alias and stub are unique to executable phar archives.

It is important to note that when creating a Phar archive, the full path should be passed to the Phar object constructor. Relative paths will fail to initialize.

Assuming that $p is a Phar object initialized as follows:

<?php
$p 
= new Phar('/path/to/myphar.phar'0'myphar.phar');
?>

An empty Phar archive will be created at /path/to/myphar.phar, or if /path/to/myphar.phar already exists, it will be opened again. The literal myphar.phar demonstrates the concept of an alias that can be used to reference /path/to/myphar.phar in URLs as in:

<?php
// these two calls to file_get_contents() are equivalent if
// /path/to/myphar.phar has an explicit alias of "myphar.phar"
// in its manifest, or if the phar was initialized with the
// previous example's Phar object setup
$f file_get_contents('phar:///path/to/myphar.phar/whatever.txt');
$f file_get_contents('phar://myphar.phar/whatever.txt');
?>

With the newly created $p Phar object, the following is possible:

  • $a = $p['file.php'] creates a PharFileInfo class that refers to the contents of phar://myphar.phar/file.php
  • $p['file.php'] = $v creates a new file (phar://myphar.phar/file.php), or overwrites an existing file within myphar.phar. $v can be either a string or an open file pointer, in which case the entire contents of the file will be used to create the new file. Note that $p->addFromString('file.php', $v) is functionally equivalent to the above. Also possible is to add the contents of a file with $p->addFile('/path/to/file.php', 'file.php'). Lastly, an empty directory can be created with $p->addEmptyDir('empty').
  • isset($p['file.php']) can be used to determine whether phar://myphar.phar/file.php exists within myphar.phar.
  • unset($p['file.php']) erases phar://myphar.phar/file.php from myphar.phar.

In addition, the Phar object is the only way to access Phar-specific metadata, through Phar::getMetadata(), and the only way to set or retrieve a Phar archive's PHP loader stub through Phar::getStub() and Phar::setStub(). Additionally, compression for the entire Phar archive at once can only be manipulated using the Phar class.

The full list of Phar object functionality is documented below.

The PharFileInfo class extends the SplFileInfo class, and adds several methods for manipulating Phar-specific details of a file contained within a Phar, such as manipulating compression and metadata.

Hier Kannst Du einen Kommentar verfassen


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

PHPUnit in der Version 10 erschienen

Das beliebte Testframeworks PHPUnit ist in der Version 10 erschienen und steht zum Download bereit ...

TheMax

Autor : TheMax
Kategorie: Software & Web-Development

Laravel 10 veröffentlicht

Laravel Version 10 steht mit einer Mindestanforderung von PHP 8.1 zum Download bereit. ...

admin

Autor : admin
Kategorie: Software & Web-Development

DeepL Erweiterung Write als beta veröffentlicht

DeepL hat eine neuen Dienst Namens „write“ veröffentlicht, der die Rechtschreibung und Grammatik mit KI verbessern soll. ...

admin

Autor : admin
Kategorie: Dies und Das

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

Aufsatz

Hallo! Ich wollte meine Entdeckung mit euch teilen - Ghostwriter​ (https://wirschreiben.at/) . Diese Seite ist eine echte Oase für Schriftstell ...

Geschrieben von Faraon23 am 08.06.2023 21:04:40
Forum: Apps und PHP Script Gesuche
Aufsatz schreiben

Brauchen Sie Hilfe bei Ihrem Aufsatz? Ich kann Ihnen mit einer Empfehlung weiterhelfen und Sie über diesen Dienst informieren Ghostwriter Deutsch ...

Geschrieben von Faraon23 am 07.06.2023 18:56:39
Forum: Apps und PHP Script Gesuche
Welche Ghostwriter Agentur kontaktieren?

WirWRITE ist eine aussergewöhnliche Plattform, die Menschen in der Schweiz mit Erfahrene und seriöse Ghostwriter für alle Fachbereiche​ (http ...

Geschrieben von John534 am 07.06.2023 15:39:37
Forum: Off-Topic Diskussionen
Formular- Antwort

Das war es, ich musste der Datei einfach nur wieder :)den Originalnamen geben. Nun funktioniert es perfekt. Danke.

Geschrieben von Dirk66 am 21.05.2023 18:19:21
Forum: PHP Developer Forum