PHP 8.1.28 Released!

Phar::__construct

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 1.0.0)

Phar::__constructСоздаёт объект Phar-архива

Описание

public Phar::__construct(string $filename, int $flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS, ?string $alias = null)

Список параметров

filename

Путь к уже существующему Phar-архиву или к архиву, который должен быть создан. Расширение в имени файла должно содержать .phar.

flags

Флаги, которые должны быть переданы в родительский класс RecursiveDirectoryIterator.

alias

Псевдоним, с помощью которого должны производиться ссылки на этот Phar-архив в вызовах, связанных с функционалом потоков.

Ошибки

Выбрасывает исключение BadMethodCallException, если был вызван дважды, и исключение UnexpectedValueException, если phar-архив не может быть открыт.

Примеры

Пример #1 Пример использования Phar::__construct()

<?php
try {
$p = new Phar('/путь/к/my.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
'my.phar');
} catch (
UnexpectedValueException $e) {
die(
'Не удалось открыть my.phar');
} catch (
BadMethodCallException $e) {
echo
'Технически это не может произойти';
}
// это теперь работает
echo file_get_contents('phar://my.phar/example.txt');
// и работает так же, как если бы мы ввели
echo file_get_contents('phar:///путь/к/my.phar/example.txt');
?>

add a note

User Contributed Notes 2 notes

up
1
myselfasunder at findmenow dot gmail dot com
13 years ago
Zip support seems to be shaky, in that just attempting to open a Zip file (created by 7-Zip) with both the 'zlib' and 'zip' extensions enabled renders the following error:

Error: Cannot convert phar archive "C:/Development/webdir/public_html/TestPhar.zip", unable to open entry "TestPhar/" contents: phar error: internal corruption of zip-based phar "C:/Development/webdir/public_html/TestPhar.zip" (local header of file "TestPhar/" does not match central directory)

Stick to GZ's and BZ2's (but don't forget to enable the BZ2 extension if necessary).

Phar can ONLY open executable Phar's and PharData can ONLY open non-executable Phar's. Both have the ability to convert between the two formats.

However, you can reference a file within a Phar regardless of whether it's executable using the Phar stream wrapper (file_get_contents('phar://<Phar file>/subdirectory/subdirectory/somefile.txt')).

Dustin Oprea
up
0
Christian
14 years ago
It's not possible to create a new archive when safe_mode is enabled! You simply return a 'Phar creation or opening failed' error.
This applies to Phar and PharData in PHP 5.3.2.
To Top