PHAR archives can package multiple resources into a single file. Phar::delete() Method addresses a specific part of that process: removes a named entry from an archive. The examples distinguish archive-level effects from member-level effects.
What Is Phar::delete() Method?
Removes a named entry from an archive.
Its syntax is:
Phar::delete(string $localName)
The parameters are:
string $localName— Path of the member inside a Phar archive.
Return value: The declared return type is true. Deleting an entry does not necessarily remove an empty parent directory automatically.
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: Deleting an entry does not necessarily remove an empty parent directory automatically.
Example 1: Basic Usage
The basic example focuses on the behavior of Phar::delete() Method: removes a named entry from an 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->addFromString("temporary.txt", "data");
$archive->delete("temporary.txt");
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::delete() Method. Notice the different checks, arguments, or output handling.
<?php
$archive = new Phar(sys_get_temp_dir() . "/remove-file.phar");
$archive->addFromString("cache/old.txt", "old");
if (isset($archive["cache/old.txt"])) { $archive->delete("cache/old.txt"); }
This variation matters because deleting an entry does not necessarily remove an empty parent directory automatically.
Practical Example: Working with Archive Entries
The following example demonstrates how Phar::delete() Method fits into a more complete file or archive workflow, including inputs and appropriate checks.
<?php
$archive = new Phar(sys_get_temp_dir() . "/cleanup.phar");
$archive->addFromString("old-report.csv", "unused");
if ($archive->offsetExists("old-report.csv")) { $archive->delete("old-report.csv"); }
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: Removes a named entry from an archive.
- Limitation: Deleting an entry does not necessarily remove an empty parent directory automatically.
- 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::delete() Method | Removes a named entry from an archive. |
Phar::extractTo() | Copies entries from the archive onto the host filesystem; delete() removes an entry inside the archive. |
Use Phar::delete() Method for the specific behavior described above. The related operation, Phar::extractTo(), 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. | Deleting an entry does not necessarily remove an empty parent directory automatically. Confirm the expected archive format, member path and read/write permissions. |
| Treating Phar::extractTo() as an interchangeable replacement for Phar::delete() Method. | Copies entries from the archive onto the host filesystem; delete() removes an entry inside the archive. |
Key Takeaways
- Removes a named entry from an archive.
- Deleting an entry does not necessarily remove an empty parent directory automatically.
- Related operation: Phar::extractTo() — Copies entries from the archive onto the host filesystem; delete() removes an entry inside the archive.
Phar::delete() Method is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.