SlideShare a Scribd company logo
Creating a PHAR
for Greater Good!
http://bit.ly/1jnCGcu
● Software Architect at
Linio
● All around nerd
● Systems Administrator
for 7 years
● @aramonc in all the
places
About Me
like a Java JAR, but for PHP
PHp ARchive
Available since PHP 5.3
● Composer
● PHPUnit
● Magento
● Pear
● phpDox
● Phing
Used by ...
Common Uses ...
● Command Line Utilities
● Libraries
● Whole Applications
● Frameworks
● Creating Tar & Zip files
Composed of ...
● A Stub
● A manifest describing the contents
● The file contents
● A signature (optional, phar format only)
phar.readonly = Off
<?php
$baseDir = realpath(dirname(__DIR__));
$saveLoc = $baseDir . “/build/backup.phar”;
$alias = “backup”;
$flags = FileSystemIterator::CURRENT_AS_FILEINFO |
FileSystemIterator::KEY_AS_PATHNAME;
$phar = new Phar($saveLoc, $flags, $alias);
The Compiler Script
<?php
$phar->startBuffering();
$autoload = $baseDir . '/vendor/autoload.php';
$runner = $baseDir . '/bin/backup_runner.php';
$phar->addFile($autoload, '/vendor/autoload.php');
$phar->addFile($runner, '/bin/backups.php');
The Compiler Script
<?php
/** @var SPLFileInfo $file */
foreach($files as $file) {
$path = $file->getRealPath();
$path = str_replace($baseDir . ‘/’, '', $path);
$path = strtr($path, '', '/');
The Compiler Script
<?php
if($file->isDir()) {
$phar->addEmptyDir('/' . $path);
}
if($file->isFile() && !$file->isDir()) {
$phar->addFile($file->getRealPath(), $path);
}
}
The Compiler Script
<?php
$phar->buildFromDirectory($baseDir);
// OR
$phar->buildFromIterator($dirIterator);
The Compiler Script … an Alternative
<?php
$stub = file_get_contents($baseDir . '/bin/stub.php');
$phar->setStub($stub);
$phar->stopBuffering();
The Compiler Script
#!/usr/bin/env php
<?php
set_time_limit(3000);
Phar::mapPhar("backup");
require "phar://backup/bin/backups.php";
__HALT_COMPILER();
The Stub
$> php backup.phar
Using the PHAR
<?php
include 'phar://coollibrary.phar/internal/file.php';
header('Content-type: image/jpeg');
// phars can be accessed by full path or by alias
echo file_get_contents('phar:///fullpath/to/coollibrary.
phar/images/wow.jpg');
$> phar help-list
add compress delete extract help
help-list info list meta-del meta-get
meta-set pack sign stub-get
stub-set tree version
Debugging
Creating a Tar or Zip file
● Can be done with phar.readonly = 1
● Should not have a stub or alias
● Should use tar or zip extensions
<?php
$tar = new Phar($saveLoc, $flags, $alias, Phar::TAR);
$tar->compress(Phar::GZ)
$zip = new Phar($saveLoc, $flags, $alias, Phar::ZIP);
$zip->compress(Phar::BZ2)
Creating a Tar or Zip file
The box Project
● https://github.com/box-project/
● Framework for creating Phar files
● Lots of utility functions & classes for the
compiler script
● Not part of your project
● Box 3 has been in development for over a
year
A word on performance
● APC does not support PHAR
● Using the PHAR format is faster than Tar or
Zip
● Avoid compression if you’re going to use it
on the web
Resources
● PHP.net PHAR Manual
● Composer Compiler
● WebPhar Tutorial
● Working with .phar files
● Iterators in PHP - by Jake Smith @jakefolio
● Backup Project
Thank you!!

More Related Content

PDF
PHP file handling
DOCX
Php files
PDF
Module 03 File Handling in C
PPT
Files and Directories in PHP
PPTX
File handling in c
PPT
Php File Operations
PPT
File handling in c
PPT
PHP - Introduction to File Handling with PHP
PHP file handling
Php files
Module 03 File Handling in C
Files and Directories in PHP
File handling in c
Php File Operations
File handling in c
PHP - Introduction to File Handling with PHP

What's hot (20)

PPT
File in c
PPSX
C programming file handling
PDF
Apache Solr for TYPO3 Components & Review 2016
PPTX
File Management in C
PDF
TYPO3 8 is here - how we keep EXT:solr uptodate with the TYPO3 core
PPT
File handling in 'C'
PPTX
Php File Operations
PPT
File handling-c programming language
ODP
Vim and Python
PPTX
File handling in C
PPT
File Management
PDF
Drupal and Open shift (and php)
PPT
php file uploading
PPT
File in C Programming
PPSX
1file handling
PPTX
File handling in C by Faixan
PDF
Files in c
File in c
C programming file handling
Apache Solr for TYPO3 Components & Review 2016
File Management in C
TYPO3 8 is here - how we keep EXT:solr uptodate with the TYPO3 core
File handling in 'C'
Php File Operations
File handling-c programming language
Vim and Python
File handling in C
File Management
Drupal and Open shift (and php)
php file uploading
File in C Programming
1file handling
File handling in C by Faixan
Files in c
Ad

Viewers also liked (20)

PDF
Phar, The PHP .exe Format
PDF
Introducción a los Archivos Phar en PHP
PPTX
PHAR better Tools
PDF
Comunicare in rete per lo sviluppo
PDF
Conservazione Sostitutiva Legal Doclite
PPTX
Premio innova s@lute2016 - InfoCert GeoSign
PDF
Fatturazione ElettronicaPA
PDF
Smpp v3 4
PDF
Contaminazione della Digital transformation: dalla banca all'assicurazione
PDF
The Angular road from 1.x to 2.0
PDF
Connected Objects are Natural Born Storytellers (LECTURE @NABA, 2015 )
PDF
WTF? Why The Future Is Up To Us.
PDF
Messaging Apps Overview
ODP
PDF
I processi di contrattualizzazione paperless
PDF
XYZ – OUTPUT – Processi
PPT
SMS-SMPP-Concepts
PPTX
Emotiv EPOC EEG Headset
PPTX
What's the Future?
PDF
Quattro riflessioni sulla comunicazione contemporanea
Phar, The PHP .exe Format
Introducción a los Archivos Phar en PHP
PHAR better Tools
Comunicare in rete per lo sviluppo
Conservazione Sostitutiva Legal Doclite
Premio innova s@lute2016 - InfoCert GeoSign
Fatturazione ElettronicaPA
Smpp v3 4
Contaminazione della Digital transformation: dalla banca all'assicurazione
The Angular road from 1.x to 2.0
Connected Objects are Natural Born Storytellers (LECTURE @NABA, 2015 )
WTF? Why The Future Is Up To Us.
Messaging Apps Overview
I processi di contrattualizzazione paperless
XYZ – OUTPUT – Processi
SMS-SMPP-Concepts
Emotiv EPOC EEG Headset
What's the Future?
Quattro riflessioni sulla comunicazione contemporanea
Ad

Similar to Creating a phar (20)

PPT
John's Top PECL Picks
PDF
Schizophrenic files
PDF
Ange Albertini and Gynvael Coldwind: Schizophrenic Files – A file that thinks...
DOCX
Files nts
PDF
Static analysis saved my code tonight
PDF
Symfony console: build awesome command line scripts with ease
PDF
Course 102: Lecture 24: Archiving and Compression of Files
PDF
Caching with Memcached and APC
PDF
PECL Picks - Extensions to make your life better
PDF
PHP Files: An Introduction
PPTX
Php core. get rid of bugs and contribute
PDF
Development Workflow Tools for Open-Source PHP Libraries
DOC
Zipnotes
PPTX
On secure application of PHP wrappers
PDF
HPDC'23 Rapidgzip
TXT
PPTX
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
PDF
PHP 7 OPCache extension review
PDF
Xdebug - Derick Rethans - Barcelona PHP Conference 2008
PDF
Pecl Picks
John's Top PECL Picks
Schizophrenic files
Ange Albertini and Gynvael Coldwind: Schizophrenic Files – A file that thinks...
Files nts
Static analysis saved my code tonight
Symfony console: build awesome command line scripts with ease
Course 102: Lecture 24: Archiving and Compression of Files
Caching with Memcached and APC
PECL Picks - Extensions to make your life better
PHP Files: An Introduction
Php core. get rid of bugs and contribute
Development Workflow Tools for Open-Source PHP Libraries
Zipnotes
On secure application of PHP wrappers
HPDC'23 Rapidgzip
Unit3IIpartpptx__2024_10_17_19_07_58 2.pptx
PHP 7 OPCache extension review
Xdebug - Derick Rethans - Barcelona PHP Conference 2008
Pecl Picks

More from Adrian Cardenas (7)

PPTX
Iterators & generators: practical uses in memory management
PPTX
Http/2 lightning
PPTX
PDF
First there was the command line
PDF
Learning the command line
PDF
Conquering the Command Line
PDF
Communicating on the web
Iterators & generators: practical uses in memory management
Http/2 lightning
First there was the command line
Learning the command line
Conquering the Command Line
Communicating on the web

Recently uploaded (20)

PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
history of c programming in notes for students .pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPT
Introduction Database Management System for Course Database
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Odoo Companies in India – Driving Business Transformation.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
history of c programming in notes for students .pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
L1 - Introduction to python Backend.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction Database Management System for Course Database
PTS Company Brochure 2025 (1).pdf.......
Online Work Permit System for Fast Permit Processing
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo POS Development Services by CandidRoot Solutions
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Adobe Illustrator 28.6 Crack My Vision of Vector Design
ai tools demonstartion for schools and inter college
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo Companies in India – Driving Business Transformation.pdf

Creating a phar