Content · Programming · PHP

PHP Phar::extractTo() Method: Usage and Examples

Extracts selected archive entries into a destination directory.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

PHAR archives can package multiple resources into a single file. Phar::extractTo() Method addresses a specific part of that process: extracts selected archive entries into a destination directory. The examples distinguish archive-level effects from member-level effects.

What Is Phar::extractTo() Method?


Extracts selected archive entries into a destination directory.

Its syntax is:

Phar::extractTo(string $directory, array|string|null $files = null, bool $overwrite = false)

The parameters are:

  • string $directory — A directory path or archive-internal directory name.
  • array|string|null $files = null — Archive member name or names to extract; null selects all entries.
  • bool $overwrite = false — Whether existing destination files may be replaced.

Return value: The declared return type is bool. Treat untrusted archives as untrusted input and prevent traversal or overwriting important files.

Requirements: The Phar extension must be enabled. Creating or modifying executable PHAR archives generally requires the phar.readonly setting to be disabled before startup; do not change server configuration just to view this guide.

Important: Treat untrusted archives as untrusted input and prevent traversal or overwriting important files.

Example 1: Basic Usage


The basic example focuses on the behavior of Phar::extractTo() Method: extracts selected archive entries into a destination directory. Inputs are defined locally or shown as application-controlled paths.

<?php

$archivePath = sys_get_temp_dir() . "/example-package.phar";
$archive = new Phar($archivePath);
$archive->addFromString("manual.txt", "example");
$archive->extractTo(__DIR__ . "/extract-output", ["manual.txt"], false);

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for Phar::extractTo() Method. Notice the different checks, arguments, or output handling.

<?php

$archive = new Phar(__DIR__ . "/trusted-package.phar");
$archive->extractTo(__DIR__ . "/private-extracted", ["docs/readme.txt"], false);

This variation matters because treat untrusted archives as untrusted input and prevent traversal or overwriting important files.

Practical Example: Working with Archive Entries


The following example demonstrates how Phar::extractTo() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$archive = new Phar(__DIR__ . "/trusted-files.phar");
$requestedEntry = "docs/manual.txt";
if (!$archive->offsetExists($requestedEntry)) { throw new RuntimeException("Requested entry missing"); }
$archive->extractTo(__DIR__ . "/output-docs", [$requestedEntry], false);

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Extracts selected archive entries into a destination directory.
  • Limitation: Treat untrusted archives as untrusted input and prevent traversal or overwriting important files.
  • Availability: The Phar extension must be enabled. Creating or modifying executable PHAR archives generally requires the phar.readonly setting to be disabled before startup; do not change server configuration just to view this guide.

Comparison With Related Functions


Function or MethodDifference
Phar::extractTo() MethodExtracts selected archive entries into a destination directory.
Phar::offsetGet()Reads archive member metadata/content through a PharFileInfo object without extracting to disk.

Use Phar::extractTo() Method for the specific behavior described above. The related operation, Phar::offsetGet(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Trusting input paths, file extensions, or destination directories without validation.Treat untrusted archives as untrusted input and prevent traversal or overwriting important files. Validate the source, output location and permissions before processing data.
Treating Phar::offsetGet() as an interchangeable replacement for Phar::extractTo() Method.Reads archive member metadata/content through a PharFileInfo object without extracting to disk.

Key Takeaways


  • Extracts selected archive entries into a destination directory.
  • Treat untrusted archives as untrusted input and prevent traversal or overwriting important files.
  • Related operation: Phar::offsetGet() — Reads archive member metadata/content through a PharFileInfo object without extracting to disk.

Phar::extractTo() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.