PHAR (file format)

Last updated
PHP Archive
Filename extension
.phar
Developed by PHP, Davey Shafik, Greg Beaver, Marcus Börger
Type of format File archive, data compression
Extended from ZIP, tar

In software, a PHAR (PHP Archive) file is a package format to enable distribution of applications and libraries by bundling many PHP code files and other resources (e.g. images, stylesheets, etc.) into a single archive file.

Contents

PHAR files may be in one of three formats: tar, and ZIP, which are compatible with their respective tooling, and a custom PHAR format. Regardless of the format used, all PHAR files use the .phar file extension. Tar and Zip format archives may be created and unpacked using standard tar and zip utilities, while the PHAR format requires custom PHP code using the PHAR extension for PHP, or the PEAR PHP_Archive package.

History

Created in 2004, PHAR files were influenced [1] by Java's JAR file format with the aim of speeding up deployment of applications via FTP. [1] Rather than using the ZIP file format the simpler tar format was used, and the first PHAR was created using the tar command line utility [1] as a proof of concept. On June 14, 2004 [2] the initial PHP_Archive package was proposed to PEAR and on December 13, 2004 it was accepted unanimously. [3]

PHAR Extension

In 2007, PHP_Archive was ported to C and released as a PECL extension. [4] The extension was included and enabled by default in PHP 5.3.0. [5] [6]

Design

A PHAR file allows for a Tar, Zip or PHAR formatted archive. Regardless of format, each archive contains three sections:

  1. Stub — A PHP file that will bootstrap the archive. The stub must contain the __HALT_COMPILER(); token, and the default stub includes the ability to run a PHAR with or without the PHP extension enabled [7]
  2. Manifest — The manifest details the contents of the archive
  3. File Contents — The original files that are included in the archive

Additionally, the PHAR format may also include a signature for verifying PHAR integrity.

Manifest

The manifest contains meta-data information about the archive, and its contents. The binary format is intended to be efficient to parse in both PHP and C. It consists of fixed length segments, in addition to pairs of length specifications followed by variable length segments. [8] Each file has its own manifest within a segment of the global manifest. The current format is version 1.1.1.

Compression

All three formats have support for compression, however only the PHAR format supports both per-file and whole archive compression. Zip and Tar formats only support per-file and whole file compression respectively.

Executing PHAR files

Assuming the PHAR extension is enabled, all PHAR files may be executed simply by executing them with the PHP interpreter ("php file.phar"). If the PHAR extension is not enabled, only PHAR format can be executed. [9]

Additionally, it is possible to mark a PHAR file as executable, and to add an appropriate shebang to make the PHAR executable directly.

Usage

While PHAR was originally intended for web usage, it is often used by command line utilities. Popular applications distributed in PHAR format, include Composer and PHPUnit.

Related Research Articles

PKZIP File archiving software

PKZIP is a file archiving computer program, notable for introducing the popular ZIP file format. PKZIP was first introduced for MS-DOS on the IBM-PC compatible platform in 1989. Since then versions have been released for a number of other architectures and operating systems. PKZIP was originally written by Phil Katz and marketed by his company PKWARE, Inc, with both of them bearing his initials: 'PK'.

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization.

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The ZIP file format permits a number of compression algorithms, though DEFLATE is the most common. This format was originally created in 1989 and was first implemented in PKWARE, Inc.'s PKZIP utility, as a replacement for the previous ARC compression format by Thom Henderson. The ZIP format was then quickly supported by many software utilities other than PKZIP. Microsoft has included built-in ZIP support in versions of Microsoft Windows since 1998. Apple has included built-in ZIP support in Mac OS X 10.3 and later. Most free operating systems have built in support for ZIP in similar manners to Windows and Mac OS X.

JAR (file format) Java archive file format

A JAR is a package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution.

RAR is a proprietary archive file format that supports data compression, error recovery and file spanning. It was developed in 1993 by Russian software engineer Eugene Roshal and the RAR software is licensed by win.rar GmbH.

7-Zip Open-source file archiver

7-Zip is a free and open-source file archiver, a utility used to place groups of files within compressed containers known as "archives". It is developed by Igor Pavlov and was first released in 1999. 7-Zip uses its own 7z archive format, but can read and write several other archive formats. The program can be used from a command-line interface as the command p7zip, or through a graphical user interface that also features shell integration. Most of the 7-Zip source code is under the GNU LGPL license; the unRAR code, however, is under the GNU LGPL with an "unRAR restriction", which states that developers are not permitted to use the code to reverse-engineer the RAR compression algorithm. Since 7-Zip 21.01 alpha, it adds preliminary Linux support in the upstream instead of the p7zip project.

7z is a compressed archive file format that supports several different data compression, encryption and pre-processing algorithms. The 7z format initially appeared as implemented by the 7-Zip archiver. The 7-Zip program is publicly available under the terms of the GNU Lesser General Public License. The LZMA SDK 4.62 was placed in the public domain in December 2008. The latest stable version of 7-Zip and LZMA SDK is version 19.00.

StuffIt was a family of computer software utilities for archiving and compressing files. Originally produced for the Macintosh, versions for Microsoft Windows, Linux (x86), and Sun Solaris were later created. The proprietary compression format used by the StuffIt utilities is also termed StuffIt.

deb is the format, as well as extension of the software package format for the Linux distribution Debian and its derivatives.

.exe is a common filename extension denoting an executable file for Microsoft Windows.

pax (command)

pax is an archiving utility available for various operating systems and defined since 1995. Rather than sort out the incompatible options that have crept up between tar and cpio, along with their implementations across various versions of Unix, the IEEE designed a new archive utility that could support various archive formats with useful options from both archivers. The pax command is available on Unix and Unix-like operating systems and on IBM i, Microsoft Windows NT, and Windows 2000.

The PHP Extension and Application Repository, or PEAR, is a repository of PHP software code. Stig S. Bakken founded the PEAR project in 1999 to promote the re-use of code that performs common functions. The project seeks to provide a structured library of code, maintain a system for distributing code and for managing code packages, and promote a standard coding style. Though community-driven, the PEAR project has a PEAR Group which serves as the governing body and takes care of administrative tasks. Each PEAR code package comprises an independent project under the PEAR umbrella. It has its own development team, versioning-control and documentation.

WinRAR File archiver

WinRAR is a trialware file archiver utility for Windows, developed by Eugene Roshal of win.rar GmbH. It can create and view archives in RAR or ZIP file formats, and unpack numerous archive file formats. To enable the user to test the integrity of archives, WinRAR embeds CRC32 or BLAKE2 checksums for each file in each archive. WinRAR supports creating encrypted, multi-part and self-extracting archives.

The following tables compare general and technical information for a number of file archivers. Please see the individual products' articles for further information. They are neither all-inclusive nor are some entries necessarily up to date. Unless otherwise specified in the footnotes section, comparisons are based on the stable versions—without add-ons, extensions or external programs.

In computing, an archive file is a computer file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space. Archive files often store directory structures, error detection and correction information, arbitrary comments, and sometimes use built-in encryption.

ZipGenius

ZipGenius is a freeware file archiver developed by The ZipGenius Team for Microsoft Windows. It is capable of handling nearly two dozen file formats, including all the most common formats, as well as password-protect archives and work directly with CD-R/RW drives. It is presented in two editions: standard and suite. While the suite edition includes optional modules of the ZipGenius project, the standard setup package simply includes the main ZipGenius application.

Self-extracting archive

A self-extracting archive (SFX/SEA) is a computer executable program which contains compressed data in an archive file combined with machine-executable program instructions to extract this information on a compatible operating system and without the necessity for a suitable extractor to be already installed on the target computer. The executable part of the file is known as the stub and the non-executable part the archive.

PeaZip File archive computer program

PeaZip is a free and open-source file manager and file archiver for Microsoft Windows, ReactOS, Linux and BSD made by Giorgio Tani. It supports its native PEA archive format and other mainstream formats, with special focus on handling open formats. It supports 211 file extensions.

lzip

lzip is a free, command-line tool for the compression of data; it employs the Lempel–Ziv–Markov chain algorithm (LZMA) with a user interface that is familiar to users of usual Unix compression tools, such as gzip and bzip2.

Apple Disk Image Disk image file format developed by Apple and commonly used by macOS

AppleDisk Image is a disk image format commonly used by the macOS operating system. When opened, an Apple Disk Image is mounted as a volume within the Macintosh Finder.

References

  1. 1 2 3 "PHP 5.3 5th Anniversary: The History of PHP Archives (PHAR Files)". blog.engineyard.com. Archived from the original on 2017-09-17. Retrieved 2016-06-16.
  2. "PEPr :: Details :: PHP_Archive". pear.php.net. Retrieved 2016-06-16.
  3. "PEPr :: Votes :: PHP_Archive". pear.php.net. Retrieved 2016-06-16.
  4. "PECL :: Package :: phar". pecl.php.net. Retrieved 2016-06-16.
  5. "PHP: News Archive - 2009". php.net. Retrieved 2016-06-16.
  6. "PHP: PHP 5 ChangeLog". php.net. Retrieved 2016-06-16.
  7. "PHP: Phar::createDefaultStub - Manual". php.net. Retrieved 2016-06-17.
  8. "PHP: Phar File Format - Manual". php.net. Retrieved 2016-06-17.
  9. Mertic, John (January 27, 2009). "What's new in PHP V5.3, Part 4, Creating and using Phar archives". IBM.com. Archived from the original on March 2, 2020. Retrieved March 3, 2020.