PHP Velho Oeste 2024

PharData::__construct

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

PharData::__construct実行可能でない tar あるいは zip アーカイブオブジェクトを作成する

説明

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

パラメータ

filename

既存の tar/zip アーカイブあるいは作成したいアーカイブへのパス。

flags

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

alias

この Phar アーカイブをストリーム機能からコールするときに使うエイリアス。

format

Phar クラスの ファイルフォーマット定数 のいずれか。

エラー / 例外

2 度コールされた場合に BadMethodCallException、 phar アーカイブのオープンに失敗した場合に UnexpectedValueException をスローします。

例1 PharData::__construct() の例

<?php
try {
$p = new PharData('/path/to/my.tar', Phar::CURRENT_AS_FILEINFO | Phar::KEY_AS_FILENAME);
} catch (
UnexpectedValueException $e) {
die(
'Could not open my.tar');
} catch (
BadMethodCallException $e) {
echo
'technically, this cannot happen';
}
echo
file_get_contents('phar:///path/to/my.tar/example.txt');
?>

add a note add a note

User Contributed Notes 1 note

up
-13
andkirby at gmail dot com
9 years ago
There is a big problem!
It doesn't work but in console (tar -xvzf packagename.tar.gz)  the file was unpacked!

$ php -r "new \PharData('d:/Downloads/commithook-master.tar.gz');"
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'phar error: "D:\Downloads\commithook-master.tar.gz" is a corrupted tar file (checksum mismatch of file "52 comment=4d08b1e311f074f7281c0f1c21912776ac51394b
")' in Command line code:1
Stack trace:
#0 Command line code(1): PharData->__construct('d:/Downloads/co...')
#1 {main}
  thrown in Command line code on line 1
To Top