How to Read ZIP File Entries in PHP with zip_read(): Step‑by‑Step Guide

zip_read() is a PHP function that reads each entry from an opened ZIP archive, returning detailed information about the entry or false when the end is reached; the guide explains its syntax, step‑by‑step usage, example code, and notes its limitation to metadata reading.

php Courses
php Courses
php Courses
How to Read ZIP File Entries in PHP with zip_read(): Step‑by‑Step Guide

zip_read() is a PHP function that reads the next entry from an opened ZIP archive and returns its details, or false when no more entries remain.

Syntax

zip_read(resource $zip)

The $zip parameter is the resource returned by zip_open().

Usage Steps

Open the ZIP file

$zip = zip_open('path/to/zipfile.zip');

Check if the file opened successfully

if ($zip) {
    // proceed with processing
} else {
    // handle error
}

Loop through entries

while ($zip_entry = zip_read($zip)) {
    // process each entry
}

Close the ZIP file

zip_close($zip);

Within the loop, each call to zip_read() returns the next entry. The resulting $zip_entry can be inspected with zip_entry_name() for the entry name, zip_entry_filesize() for its size, and other zip_entry_* functions.

Complete example

$zip = zip_open('path/to/zipfile.zip');
if ($zip) {
    while ($zip_entry = zip_read($zip)) {
        $name = zip_entry_name($zip_entry);
        $size = zip_entry_filesize($zip_entry);
        echo "Name: $name, Size: $size bytes
";
    }
    zip_close($zip);
} else {
    echo "Failed to open ZIP file
";
}

Note that zip_read() only reads entry metadata; it does not extract file contents. To extract files, use zip_entry_open() and zip_entry_read() or other extraction functions.

In summary, zip_read() is a useful PHP function for enumerating entries in a ZIP archive, enabling further processing such as retrieving names, sizes, or reading data.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

file-handlingzipzip_read
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.