Content · Programming · PHP

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

Adds an archive entry from a string already in memory.

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

PHAR archives can package multiple resources into a single file. Phar::addFromString() Method addresses a specific part of that process: adds an archive entry from a string already in memory. The examples distinguish archive-level effects from member-level effects.

What Is Phar::addFromString() Method?


Adds an archive entry from a string already in memory.

Its syntax is:

Phar::addFromString(string $localName, string $contents)

The parameters are:

  • string $localName — Path of the member inside a Phar archive.
  • string $contents — Text or bytes to store in an archive entry.

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: Do not use unsanitized archive paths with traversal components.

Example 1: Basic Usage


The basic example focuses on the behavior of Phar::addFromString() Method: adds an archive entry from a string already in memory. 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("docs/readme.txt", "Documentation content");
echo $archive["docs/readme.txt"]->getContent();

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

<?php

$archive = new Phar(sys_get_temp_dir() . "/text.phar");
$archive->addFromString("config/version.txt", "1.2.0\n");
var_dump($archive->offsetExists("config/version.txt"));

This variation matters because do not use unsanitized archive paths with traversal components.

Practical Example: Working with Archive Entries


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

<?php

$archive = new Phar(sys_get_temp_dir() . "/settings-bundle.phar");
$settings = ["environment" => "production", "version" => 2];
$archive->addFromString("config/settings.json", json_encode($settings, JSON_THROW_ON_ERROR));

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 archive entry from a string already in memory.
  • Limitation: Do not use unsanitized archive paths with traversal components.
  • 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::addFromString() MethodAdds an archive entry from a string already in memory.
Phar::addFile()Reads a filesystem file; addFromString() accepts content already in memory.

Use Phar::addFromString() Method for the specific behavior described above. The related operation, Phar::addFile(), 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.Do not use unsanitized archive paths with traversal components. Confirm the expected archive format, member path and read/write permissions.
Treating Phar::addFile() as an interchangeable replacement for Phar::addFromString() Method.Reads a filesystem file; addFromString() accepts content already in memory.

Key Takeaways


  • Adds an archive entry from a string already in memory.
  • Do not use unsanitized archive paths with traversal components.
  • Related operation: Phar::addFile() — Reads a filesystem file; addFromString() accepts content already in memory.

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