zip_open() function in PHP

The zip_open() function opens a ZIP file for reading in PHP. It returns a resource handle on success or FALSE on failure. This function is part of PHP's ZIP extension and allows you to read the contents of ZIP archives.

Syntax

zip_open(filename)

Parameters

The function accepts a single parameter ?

  • filename − The path to the ZIP file that needs to be opened for reading.

Return Value

Returns a ZIP file resource handle on success, or FALSE on failure (e.g., if the file doesn't exist or is not a valid ZIP archive).

Example

Here's how to open a ZIP file and read information about its entries ?

<?php
    $zip = zip_open("archive.zip");
    
    if ($zip) {
        while ($zip_entry = zip_read($zip)) {
            echo "File: " . zip_entry_name($zip_entry) . "<br/>";
            echo "Compressed size: " . zip_entry_compressedsize($zip_entry) . " bytes<br/>";
            echo "Actual size: " . zip_entry_filesize($zip_entry) . " bytes<br/><br/>";
        }
        zip_close($zip);
    } else {
        echo "Failed to open ZIP file";
    }
?>

Output

File: document.txt
Compressed size: 245 bytes
Actual size: 512 bytes

File: image.jpg
Compressed size: 15840 bytes
Actual size: 18920 bytes

Important Notes

The zip_open() function and related ZIP functions have been deprecated since PHP 8.0.0. It's recommended to use the modern ZipArchive class instead for better functionality and future compatibility.

Conclusion

While zip_open() provides basic ZIP file reading capabilities, it's now deprecated. Use ZipArchive for new projects as it offers more features and better error handling.

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

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements