PHP 8.3.4 Released!

Phar::__construct

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

Phar::__constructPhar アーカイブオブジェクトを作成する

説明

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

パラメータ

filename

既存の Phar アーカイブ、あるいは作成したいアーカイブへのパス。 ファイル名の拡張子は .phar にする必要があります。

flags

親クラス RecursiveDirectoryIterator に渡すフラグ。

alias

ストリーム機能をコールする場合に Phar アーカイブが参照するエイリアス。

エラー / 例外

二度コールされた場合に BadMethodCallException、 phar アーカイブがオープンできなかった場合に UnexpectedValueException がスローされます。

例1 Phar::__construct() の例

<?php
try {
$p = new Phar('/path/to/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:///path/to/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
13 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