PHAR archives can package multiple resources into a single file. Phar::addFile() Method addresses a specific part of that process: adds an existing local file to a Phar archive. The examples distinguish archive-level effects from member-level effects.
What Is Phar::addFile() Method?
Adds an existing local file to a Phar archive.
Its syntax is:
Phar::addFile(string $filename, ?string $localName = null)
The parameters are:
string $filename— The source or destination filesystem path.?string $localName = null— Path of the member inside a Phar archive.
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: Use a safe local archive name and confirm the source file is readable.
Example 1: Basic Usage
The basic example focuses on the behavior of Phar::addFile() Method: adds an existing local file to 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->addFile(__DIR__ . "/readme.txt", "docs/readme.txt");
echo $archive["docs/readme.txt"]->getFilename();
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::addFile() Method. Notice the different checks, arguments, or output handling.
<?php
$archive = new Phar(sys_get_temp_dir() . "/files.phar");
$sourcePath = __DIR__ . "/license.txt";
if (is_file($sourcePath)) { $archive->addFile($sourcePath, "legal/license.txt"); }
This variation matters because use a safe local archive name and confirm the source file is readable.
Practical Example: Working with Archive Entries
The following example demonstrates how Phar::addFile() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$archive = new Phar(sys_get_temp_dir() . "/asset-bundle.phar");
foreach (["README.md", "LICENSE"] as $filename) {
$sourcePath = __DIR__ . "/" . $filename;
if (is_file($sourcePath)) { $archive->addFile($sourcePath, "docs/" . $filename); }
}
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 existing local file to a Phar archive.
- Limitation: Use a safe local archive name and confirm the source file is readable.
- 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 Method | Difference |
|---|---|
Phar::addFile() Method | Adds an existing local file to a Phar archive. |
Phar::addFromString() | Creates an archive entry from a PHP string instead of a filesystem file. |
Use Phar::addFile() 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
| Mistake | Correct Approach |
|---|---|
| Confusing archive-level operations with operations on individual entries. | Use a safe local archive name and confirm the source file is readable. Confirm the expected archive format, member path and read/write permissions. |
| Treating Phar::addFromString() as an interchangeable replacement for Phar::addFile() Method. | Creates an archive entry from a PHP string instead of a filesystem file. |
Key Takeaways
- Adds an existing local file to a Phar archive.
- Use a safe local archive name and confirm the source file is readable.
- Related operation: Phar::addFromString() — Creates an archive entry from a PHP string instead of a filesystem file.
Phar::addFile() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.