Content · Programming · PHP

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

Builds an archive by collecting files from a directory using an optional regex.

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

PHAR archives can package multiple resources into a single file. Phar::buildFromDirectory() Method addresses a specific part of that process: builds an archive by collecting files from a directory using an optional regex. The examples distinguish archive-level effects from member-level effects.

What Is Phar::buildFromDirectory() Method?


Builds an archive by collecting files from a directory using an optional regex.

Its syntax is:

Phar::buildFromDirectory(string $directory, string $pattern = "")

The parameters are:

  • string $directory — A directory path or archive-internal directory name.
  • string $pattern = "" — A matching pattern; glob() uses filename wildcards, while Phar::buildFromDirectory() accepts a regular expression.

Return value: The declared return type is array. Avoid accidentally bundling secrets, credentials and development 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: Avoid accidentally bundling secrets, credentials and development files.

Example 1: Basic Usage


The basic example focuses on the behavior of Phar::buildFromDirectory() Method: builds an archive by collecting files from a directory using an optional regex. Inputs are defined locally or shown as application-controlled paths.

<?php

$archivePath = sys_get_temp_dir() . "/example-package.phar";
$archive = new Phar($archivePath);
$filesAdded = $archive->buildFromDirectory(__DIR__ . "/package", "/\.php$/");
echo count($filesAdded);

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::buildFromDirectory() Method. Notice the different checks, arguments, or output handling.

<?php

$archive = new Phar(sys_get_temp_dir() . "/source.phar");
$addedFiles = $archive->buildFromDirectory(__DIR__ . "/src");
foreach (array_keys($addedFiles) as $filePath) { echo basename($filePath) . "\n"; }

This variation matters because avoid accidentally bundling secrets, credentials and development files.

Practical Example: Working with Archive Entries


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

<?php

$archive = new Phar(sys_get_temp_dir() . "/app-files.phar");
$sourcePath = __DIR__ . "/src";
if (!is_dir($sourcePath)) { throw new RuntimeException("Source directory missing"); }
$added = $archive->buildFromDirectory($sourcePath, "/\.php$/");
echo "Archived " . count($added) . " PHP files";

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: Builds an archive by collecting files from a directory using an optional regex.
  • Limitation: Avoid accidentally bundling secrets, credentials and development 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::buildFromDirectory() MethodBuilds an archive by collecting files from a directory using an optional regex.
Phar::buildFromIterator()Selects archive entries using a Traversable rather than scanning a directory with an optional regex.

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

Common Beginner Mistakes


MistakeCorrect Approach
Confusing archive-level operations with operations on individual entries.Avoid accidentally bundling secrets, credentials and development files. Confirm the expected archive format, member path and read/write permissions.
Treating Phar::buildFromIterator() as an interchangeable replacement for Phar::buildFromDirectory() Method.Selects archive entries using a Traversable rather than scanning a directory with an optional regex.

Key Takeaways


  • Builds an archive by collecting files from a directory using an optional regex.
  • Avoid accidentally bundling secrets, credentials and development files.
  • Related operation: Phar::buildFromIterator() — Selects archive entries using a Traversable rather than scanning a directory with an optional regex.

Phar::buildFromDirectory() 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.