zip_read() function in PHP

The zip_read() function reads the next entry in a ZIP file archive. It iterates through entries sequentially, returning a resource for each file or directory found within the archive.

Syntax

zip_read(zip)

Parameters

  • zip − The zip resource to read, opened with zip_open()

Return Value

The zip_read() function returns a resource containing a file within the zip archive on success, or FALSE when there are no more entries to read.

Example

The following example demonstrates reading entries from a ZIP file. We have a zip file "new.zip" with the following files ?

amit.txt
peter.txt
result.html
demo.java
settings.ini
<?php
    $zip = zip_open("new.zip");
    if ($zip) {
        while ($zip_entry = zip_read($zip)) {
            echo "File Name = ". zip_entry_name($zip_entry). "<br/>";
        }
        zip_close($zip);
    }
?>
File Name = amit.txt
File Name = peter.txt
File Name = result.html
File Name = demo.java
File Name = settings.ini

Error Handling

It's good practice to check if the zip file opens successfully and handle potential errors ?

<?php
    $zip = zip_open("new.zip");
    if (is_resource($zip)) {
        while ($zip_entry = zip_read($zip)) {
            if ($zip_entry !== FALSE) {
                echo "Entry: " . zip_entry_name($zip_entry) . "<br>";
            }
        }
        zip_close($zip);
    } else {
        echo "Error opening ZIP file: " . $zip;
    }
?>

Conclusion

The zip_read() function provides a simple way to iterate through ZIP archive entries sequentially. Note that these zip functions are deprecated in favor of the ZipArchive class in modern PHP versions.

Updated on: 2026-03-15T07:31:25+05:30

89 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements