zip_entry_open() function in PHP

The zip_entry_open() function is used to open a zip archive entry for reading. This function is part of PHP's legacy Zip extension and allows you to access individual files within a ZIP archive.

Syntax

zip_entry_open(zip_file, zip_entry, mode)

Parameters

  • zip_file − The zip file resource to be read

  • zip_entry − The zip entry resource to open

  • mode − The type of access needed for zip archive (e.g., "r" for read, "rb" for read binary)

Return Value

The zip_entry_open() function returns TRUE on success, or FALSE on failure.

Example

Here's how to use zip_entry_open() to open and read a zip entry −

<?php
    $zip_file = zip_open("new.zip");
    
    if ($zip_file) {
        $zip_entry = zip_read($zip_file);
        
        if ($zip_entry) {
            // Open the zip entry for reading
            $result = zip_entry_open($zip_file, $zip_entry, "rb");
            
            if ($result) {
                $filename = zip_entry_name($zip_entry);
                echo "Zip file: " . $filename . " is opened now!<br>";
                
                // Close the entry
                $flag = zip_entry_close($zip_entry);
                if ($flag) {
                    echo $filename . " closed!";
                }
            }
        }
        
        zip_close($zip_file);
    }
?>
Zip file: new/one.txt is opened now!
new/one.txt closed!

Key Points

  • This function requires a valid zip file resource and zip entry resource

  • Always check return values to handle errors properly

  • Use zip_entry_close() to close the entry after opening

  • The legacy Zip extension is deprecated; consider using ZipArchive class instead

Conclusion

The zip_entry_open() function provides a way to open individual entries within ZIP archives for reading. While functional, modern PHP applications should use the ZipArchive class for better performance and features.

Updated on: 2026-03-15T07:30:53+05:30

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements