How to Read Zip File Using PHP

Creating and extracting zip file are some of the most important functions in PHP. However, sometimes the developer wants to read the zip file without extracting it. In this tutorial, we will see how to read a zip file using PHP.

If you want to see how to create or extract zip file, use the links given below.
How to create a zip file using PHP
How to extract a zip file using PHP

The code is written in two files; index.php where user uploads the zip file and read.php where the file is uploaded and all the files in it are read and echoed.

index.php – Uploading File

<form action="read.php" method="post" enctype="multipart/form-data">

    <label>Upload zip file</label> 
    <input name="zip" type="file" size="30" required /><br><span>(Max file size : 100Kb)</span><br><br>

    <input name="submit" type="submit" id="submit" class="submit" value="Submit" />


readZip Function

// $target_file is the uploaded file's name and location function readZip($target_file){ // Opening the zip file $zip = zip_open($target_file); if ($zip){ // Getting all the file names inside the zip file while ($zip_entry = zip_read($zip)){ echo "<p>"; // Printing the file name echo "<h3 style='color:green;'>Name: " . zip_entry_name($zip_entry) . "</h3>"; // Opening the file if (zip_entry_open($zip, $zip_entry)){ echo "<h4 style='color:blue;'>File Contents:</h4>"; // Getting the contents $contents = zip_entry_read($zip_entry); // Printing the contents of the file echo $contents; echo "<br /><br/>"; // Closing the file zip_entry_close($zip_entry); } echo "</p>"; } // Closing the zip file zip_close($zip); } }

Explanation of Code

  • User uploads the zip file which is uploaded to the files folder. (Full code given below)
  • readZip function is called and the file name is passed to the function. The variable $target_file is used for this purpose.
  • Inside the readZip function, the zip file is opened using zip_open($target_file).
  • $zip_entry = zip_read($zip) is used to read all the files iteratively.
  • zip_entry_name($zip_entry) provides the file name.
  • zip_entry_open($zip, $zip_entry) opens the file.
  • zip_entry_read($zip_entry) gets the contents of the file which are echoed or printed on the page.
  • zip_entry_close($zip_entry) is used to close the file.
  • When all the files have been read, zip_close($zip) is used to close the zip file. – Reading Zip File


// Include the readZip function explained above here


        $file_name = $_FILES['zip']['name'];
            echo "<p style='color:red;font-size:15px;'>Upload zip file.</p>";
            $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
                echo "<p style='color:red;font-size:15px;'>Upoad file with .zip extension only</p>";

                    echo "<p style='color:red;font-size:15px;'>Maximum file size allowed is 100Kb.</p>";
                    $tmp = $_FILES['zip']['tmp_name'];

                    // Directory where zip files are uploaded
                    $target = "Your target directory here/files/";

                    $target_file = $target.basename($file_name,'.zip').'-'.time().'.zip';
                    // basename($file_name,'.zip') removes extension of the file leaving only the name

                    if (move_uploaded_file($tmp, $target_file)) {
                        /**********************Reading zip file**********************/
                    else {
                        echo "<p style='color:red;font-size:15px;'>Sorry, there was an error uploading your file.</p>";



Index Page

Upload zip

Uploaded File Contents

Uploaded file contents

Original Zip File Contents

Original Zip File Contents

Please feel free to download the source code for reference.

View Demo Download source

This content has been helpful to you?

Thanks for contributing!

Yes No