Content · Programming · PHP

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

Adds an empty directory entry inside a Phar archive.

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

PHAR archives can package multiple resources into a single file. Phar::addEmptyDir() Method addresses a specific part of that process: adds an empty directory entry inside a Phar archive. The examples distinguish archive-level effects from member-level effects.

What Is Phar::addEmptyDir() Method?


Adds an empty directory entry inside a Phar archive.

Its syntax is:

Phar::addEmptyDir(string $directory)

The parameters are:

  • string $directory — A directory path or archive-internal directory name.

Return value: This method returns no value.

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: PHAR writing usually requires phar.readonly=0 for executable archives.

Example 1: Basic Usage


The basic example focuses on the behavior of Phar::addEmptyDir() Method: adds an empty directory entry inside a Phar archive. Inputs are defined locally or shown as application-controlled paths.

<?php

$archivePath = sys_get_temp_dir() . "/example-package.phar";
$archive = new Phar($archivePath);
$archive->addEmptyDir("assets/images");
var_dump(isset($archive["assets/images"]));

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

<?php

$archivePath = sys_get_temp_dir() . "/docs.phar";
$archive = new Phar($archivePath);
$archive->addEmptyDir("reports/2026");
var_dump($archive->offsetExists("reports/2026"));

This variation matters because pHAR writing usually requires phar.readonly=0 for executable archives.

Practical Example: Working with Archive Entries


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

<?php

$archive = new Phar(sys_get_temp_dir() . "/help-manual.phar");
foreach (["docs", "docs/screenshots", "resources"] as $directoryName) {
    if (!$archive->offsetExists($directoryName)) { $archive->addEmptyDir($directoryName); }
}

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: Adds an empty directory entry inside a Phar archive.
  • Limitation: PHAR writing usually requires phar.readonly=0 for executable archives.
  • 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::addEmptyDir() MethodAdds an empty directory entry inside a Phar archive.
Phar::addFromString()Creates a member entry containing data, whereas addEmptyDir() adds only a directory entry.

Use Phar::addEmptyDir() Method for the specific behavior described above. The related operation, Phar::addFromString(), 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.PHAR writing usually requires phar.readonly=0 for executable archives. Confirm the expected archive format, member path and read/write permissions.
Treating Phar::addFromString() as an interchangeable replacement for Phar::addEmptyDir() Method.Creates a member entry containing data, whereas addEmptyDir() adds only a directory entry.

Key Takeaways


  • Adds an empty directory entry inside a Phar archive.
  • PHAR writing usually requires phar.readonly=0 for executable archives.
  • Related operation: Phar::addFromString() — Creates a member entry containing data, whereas addEmptyDir() adds only a directory entry.

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