PHP zip_entry_name() Function

Definition
In PHP, zip_entry_name() returns the name of a ZIP entry, including any folder path inside the archive.
It is deprecated as of PHP 8.0. With ZipArchive, call getNameIndex($i) or read the name key from statIndex().
Syntax
Call zip_entry_name() with the values you need. Start with this line.
// Deprecated (PHP 8.0+)
$name = zip_entry_name($entry);
// Modern equivalent
$name = $zip->getNameIndex(0);
| Call | What it does |
|---|
zip_entry_name($entry) | Deprecated: returns the path and file name of the current entry. |
@zip_entry_name($entry) | Deprecated: the @ operator hides the E_DEPRECATED notice only. |
$zip->getNameIndex($i) | Modern ZipArchive: returns the entry name at that index, or false when the index is out of range. |
zip_entry_name() is deprecated in PHP 8.0. ZipArchive::getNameIndex() does the same job. Folder entries end with a forward slash, such as images/.
Library declarations
zip_entry_name(resource $zip_entry): string|false
Parameters
| Parameter | Description |
|---|
| zip_entry | Required. A directory entry resource returned by zip_read(). In ZipArchive you pass a numeric index to getNameIndex() instead. |
Return Value
| Return Value | Description |
|---|
| string | The entry name, for example docs/readme.txt. |
| false | The name could not be read. |
PHP Examples
Example 1 — List Entry Names
Print the index and name of every entry in a small temp archive.
<?php
function makeDemoZip(): string
{
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
$zip = new ZipArchive();
if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
die("Cannot create zip\n");
}
$zip->addFromString('hello.txt', "Hello zip\n");
$zip->addFromString('docs/readme.txt', "Read me\n");
$zip->close();
return $path;
}
// Legacy (deprecated since PHP 8.0):
// while ($entry = zip_read($zip)) { echo zip_entry_name($entry); }
$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);
for ($i = 0; $i < $zip->numFiles; $i++) {
echo $i, ': ', $zip->getNameIndex($i), PHP_EOL;
}
$zip->close();
unlink($path);
0: hello.txt
1: docs/readme.txt
How It Works
- Create the demo archive and open it.
- Loop from zero up to numFiles.
- Call getNameIndex() for each index.
- Print the index and name, then close and delete the archive.
Example 2 — Folders and Missing Indexes
Folder entries end with a slash, and an index that does not exist returns false.
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addEmptyDir('images');
$zip->addFromString('images/logo.txt', 'logo');
$zip->close();
$zip = new ZipArchive();
$zip->open($path);
for ($i = 0; $i < $zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
$type = substr($name, -1) === '/' ? 'directory' : 'file';
echo $name, ' (', $type, ')', PHP_EOL;
}
echo 'Index 5: ', $zip->getNameIndex(5) === false ? 'not found' : 'found', PHP_EOL;
$zip->close();
unlink($path);
images/ (directory)
images/logo.txt (file)
Index 5: not found
How It Works
- Add an empty folder and a file inside it.
- Reopen the archive and read each name with getNameIndex().
- Treat a trailing slash as a directory marker.
- Ask for index 5 and confirm that false means not found.
Frequently Asked Questions
Yes. It belongs to the procedural zip_* API that is deprecated as of PHP 8.0. Use ZipArchive::getNameIndex() instead.
Yes. A name like docs/readme.txt includes its folder. Use basename() or pathinfo() if you only want the file name.
Folder entries end with a forward slash, for example images/.
It returns false. Loop up to numFiles to stay in range.
Not for untrusted archives. Check for .. and absolute paths before writing any file. Or use extractTo() on archives you trust.
Next: zip_entry_open()
zip_entry_open() opens a ZIP entry from an archive so that you can read its data with zip_.
zip_entry_open() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful