Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

PHP phar://

Giới thiệu

Wrapper phar:// có sẵn từ PHP 5.3.0. Phar là PHP Archive. Dùng để phân phối ứng dụng/thư viện PHP và chạy như file PHP bình thường. Wrapper phar:// hỗ trợ mở file với fopen() đọc/ghi, đổi tên, thao tác thư mục opendir(), tạo/xóa thư mục.

Lớp Phar đóng gói tài nguyên ứng dụng trong thư mục thành kho phar. Để đọc, kho này đặt trong wrapper phar://.

Tạo kho phar

Đảm bảo phar.readonly trong php.ini = 0. Tạo thư mục src chứa tài nguyên. Tạo index.php:

<?php echo "phar application started"; ?>

Dùng đối tượng Phar tạo kho chứa file trong src với buildFromDirectory(). Chỉ định index.php làm setDefaultStub:

<?php // php.ini phar.readonly = 0 $pharFile = 'app.phar'; if (file_exists($pharFile)) unlink($pharFile); if (file_exists($pharFile . '.gz')) unlink($pharFile . '.gz'); $p = new Phar($pharFile); $p->buildFromDirectory('src/'); $p->setDefaultStub('index.php', '/index.php'); $p->compress(Phar::GZ); echo "$pharFile successfully created"; ?>

Chạy từ dòng lệnh:

php create-phar.php

Tạo app.phar. Chạy kho phar:

php app.phar

Dùng wrapper phar://

<?php echo file_get_contents('phar://app.phar/index.php'); ?>

Hiển thị nội dung index.php.