Welcome to Abdul Malik Ikhsan's Blog

PHP 5.3 : Using Phar extension to archive php files

Posted in Tutorial PHP by samsonasik on May 3, 2012

Phar extension menyediakan cara mengkompresi aplikasi PHP kita ke dalam single file yang disebut “phar” ( PHP Archive ) untuk memudahkan distribusi dan instalasi. Phar dapat dikompresi dalam bentuk zip, gzip, maupun tar.gz.


Cara Kompresi :

$archive = new Phar('new_phar.phar');
$archive->buildFromDirectory(
    '/path/to/folder/will/be/compressing'
);
$archive->convertToExecutable(Phar::TAR, Phar::BZ2); // makes new_phar.phar.tar.bz2
$archive->convertToExecutable(Phar::TAR, Phar::GZ); // makes new_phar.phar.tar.gz
$archive->convertToExecutable(Phar::ZIP); //makes new_phar.phar.zip

Nah, kalau sudah, cara memanggil file di dalam archive sebagai berikut :

//how to include file in phar archive ?
$path = '/path/to/phar';
include 'phar:///'.$path.'/new_phar.phar.zip/file.php';

Terus, kalau mau meng-extract ? Berikut caranya :

//so how to extract ? 😉
$archive->extractTo('/path/to/extracted');

Gambar :
1. http://www.arvixe.com/images/hosting/php-5-3.gif

Referensi :
1. http://www.slideboom.com/presentations/26182/PHP-5.3-Part-3—Introducing-PHAR
2. http://php.net/manual/en/intro.phar.php
3. http://www.slideshare.net/nateabele/practical-php-53

2 Responses

Subscribe to comments with RSS.

  1. Sapatu Butut said, on May 28, 2012 at 2:59 pm

    pada intinya hampir sama dengan membuat zip atau rar biasa ya?
    cuma ini pakai library php?

    atau ada keuntungan lainnya?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: