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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
