Phar::__construct

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

Phar::__constructConstrói um objeto de arquivo Phar

Descrição

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

Parâmetros

filename

Caminho para um arquivo Phar existente ou um arquivo a ser criado. A extensão do nome do arquivo deve conter .phar.

flags

Sinalizadores a serem passados ​​para a classe pai RecursiveDirectoryIterator.

alias

Apelido ​​com o qual este arquivo Phar deve ser referenciado em chamadas para funcionalidade de fluxo.

Erros/Exceções

Lança BadMethodCallException se chamado duas vezes, UnexpectedValueException se o arquivo phar não puder ser aberto.

Exemplos

Exemplo #1 Um exemplo de Phar::__construct()

<?php
try {
$p = new Phar('/caminho/para/meu.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
'meu.phar');
} catch (
UnexpectedValueException $e) {
die(
'Não foi possível abrir meu.phar');
} catch (
BadMethodCallException $e) {
echo
'tecnicamente, isso não pode acontecer';
}
// isso funciona agora
echo file_get_contents('phar://meu.phar/exemplo.txt');
// e funciona como se tivesse sido digitado
echo file_get_contents('phar:///caminho/para/meu.phar/exemplo.txt');
?>

adicione uma nota

Notas Enviadas por Usuários (em inglês) 2 notes

up
1
myselfasunder at findmenow dot gmail dot com
14 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
15 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