0

I am creating a file to extract zip files . The code snipped checks if the file extension is .zip and then if the file already exists. If not, extract the archive.

    if($fileExtension == "zip")
{
    if(is_dir("unzips/".$fileName) == false)
    {
        move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);

        $zip = new ZipArchive();
        $zip -> open("temp/".$_FILES["file"]["name"]);

        for($num = 0; $num < $zip->numFiles; $num++)
        {
            $fileInfo = $zip->statIndex($num);
            echo "Successfully Extracted: ".$fileInfo["name"];
            $zip->extractTo("unzips/".$fileName);
            echo "<br />";
        }

        zip_close($zip);

        unlink("temp/".$_FILES["file"]["name"]);
    }
    else
    {
        echo $fileName." The ZIP archive file has already been unzipped";
    }
}
else
{
    echo "Only .zip files are allowed";
}

Here's the full code for index.php

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unzipping Zip File</title>
</head>

<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><input type="submit" name="submit" value="Extract">
</form>

<?php

if(isset($_POST['submit']))
{
    $array = explode(".",$_FILES["file"]["name"]);
    $fileName = $array[0];
    $fileExtension = strtolower(end($array));

    if($fileExtension == "zip")
    {
        if(is_dir("unzips/".$fileName) == false)
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
            $zip = new ZipArchive();
            $zip -> open("temp/".$_FILES["file"]["name"]);

            for($num = 0; $num < $zip->numFiles; $num++)
            {
                $fileInfo = $zip->statIndex($num);
                echo "Successfully Extracted: ".$fileInfo["name"];
                $zip->extractTo("unzips/".$fileName);
                echo "<br />";
            }

            $zip -> close();

            unlink("temp/".$_FILES["file"]["name"]);
        }
        else
        {
            echo $fileName." The ZIP archive file has already been unzipped";
        }
    }
    else
    {
        echo "Only .zip files are allowed";
    }
}

?>

<br />
<p id="end note">Please return to main menu and download individually extracted files.</p>
</body>
</html>

directory structure looks like: D:/MyWebs/extracting/index.php D:/MyWebs/extracting/unzips (final destination that contains unzipped folder) D:/MyWebs/extracting/temp (stores zip and then deletes them)

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
  • 1
    use $zip->close(); instead because zip_close() accepts a resource zip file opened with zip_open(); – Nimeshka Srimal Jul 11 '18 at 11:22
  • You have another issue in your code. You are setting an invalid value to `$fileName`, (when you get it as `$fileName = $array[0];`, you are missing the extension from it). so opening the zip file will fail. See my updated answer. – Nimeshka Srimal Jul 11 '18 at 12:36

2 Answers2

1

void zip_close ( resource $zip )

zip

A ZIP file previously opened with zip_open().

http://php.net/manual/en/function.zip-close.php

$zip is an instance of ZipArchive() class. So you cannot pass that to zip_close() as it expects a resource created with zip_open().

...............................................................................

EDIT: I noticed you have another bug in your code after you added the full source.

You have set an invalid value to $fileName variable.

$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];

When you explode the $_FILES["file"]["name"], and get only the $array[0] value, you are only getting the name of the file (without the extension).

So when you pass an invalid path to $zip->open($fileName); method, it will return you an error:

ZipArchive::close(): Invalid or uninitialized Zip object

Change the above to something like:

$array = explode(".",$_FILES["file"]["name"]);
$extractDir = $array[0];
$fileName = $_FILES["file"]["name"];
$fileExtension = strtolower(end($array));

if(is_dir("unzips/".$extractDir) == false){
   .
   .
   $zip->extractTo("unzips/".$extractDir);
   .
   .
}

This will work. Hope it helps :)

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
0

zip_close() expects parameter 1 to be resource, object given

The error message is self-explanatory.

The documentation of zip_close() says that the argument expected by zip_close() should be:

A ZIP file previously opened with zip_open().

Since you are using a ZipArchive object you have to use its methods to handle it. In particular, ZipArchive::close() is the method used to close a ZipArchive (previously open using ZipArchive::open() as you did).

Your code should look like:

$zip = new ZipArchive();
$zip->open("temp/".$_FILES["file"]["name"]);

// Do whatever you want with the content of the archive here (extract files, f.e.)

$zip->close();
axiac
  • 68,258
  • 9
  • 99
  • 134