PHP zip_entry_name() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

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.

PHP
// Deprecated (PHP 8.0+)
$name = zip_entry_name($entry);

// Modern equivalent
$name = $zip->getNameIndex(0);
CallWhat 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
PHP
zip_entry_name(resource $zip_entry): string|false

Parameters

ParameterDescription
zip_entryRequired. A directory entry resource returned by zip_read(). In ZipArchive you pass a numeric index to getNameIndex() instead.

Return Value

Return ValueDescription
stringThe entry name, for example docs/readme.txt.
falseThe 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
<?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);

How It Works

  1. Create the demo archive and open it.
  2. Loop from zero up to numFiles.
  3. Call getNameIndex() for each index.
  4. 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
<?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);

How It Works

  1. Add an empty folder and a file inside it.
  2. Reopen the archive and read each name with getNameIndex().
  3. Treat a trailing slash as a directory marker.
  4. Ask for index 5 and confirm that false means not found.

Example 3 — Split Names Into Base Name and Extension

Use pathinfo() on each entry name to get the file name and extension.

PHP
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('docs/readme.txt', 'a');
$zip->addFromString('notes/todo.md', 'b');
$zip->addFromString('logo.png', 'c');
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
for ($i = 0; $i < $zip->numFiles; $i++) {
    $name = $zip->getNameIndex($i);
    $info = pathinfo($name);
    echo $name, ' => ', $info['basename'], ' (', $info['extension'], ')', PHP_EOL;
}
$zip->close();

unlink($path);

How It Works

  1. Add three files, two of them inside folders.
  2. Reopen the archive and read each name.
  3. Pass the name to pathinfo().
  4. Print the full name, base name, and extension, then clean up.

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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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