Shiho
Shiho

Reputation: 116

How can I copy a file from the inside of a Phar to the outside

I would like to know if is it possible to copy a file from inside of a Phar to outside of it.

And how to do it if appropriate.

The copy function will be called from this same Phar.

Upvotes: 1

Views: 366

Answers (1)

Smittey
Smittey

Reputation: 2492

You can use Phar::extractTo to do this.

The documentation for this states the following:

Phar::extractTo — Extract the contents of a phar archive to a directory

With the example usage:

public bool Phar::extractTo ( string $pathto [, string|array $files [, bool $overwrite = false ]] )

Note: Your php.ini file must have phar.readonly to be set to 0 for this to work.

An example usage from the docs:

<?php
try {
    $phar = new Phar('myphar.phar');
    $phar->extractTo('/full/path'); // extract all files
    $phar->extractTo('/another/path', 'file.txt'); // extract only file.txt
    $phar->extractTo('/this/path',
        array('file1.txt', 'file2.txt')); // extract 2 files only
    $phar->extractTo('/third/path', null, true); // extract all files, and overwrite
} catch (Exception $e) {
    // handle errors
}
?>

Upvotes: 1

Related Questions