Trying to upload Zip file

Hi,

I am trying to upload a Zip file and extract it using the following code:


class Zip {

    public function extract($filename, $targetDir){

        $zip = zip_open($filename);

        if ($zip) {
            
            while ($zip_entry = zip_read($zip)) {

                $fp = fopen($targetDir . zip_entry_name($zip_entry), "w");
                
                if (zip_entry_open($zip, $zip_entry, "r")) {
                    $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                    fwrite($fp,"$buf");
                    zip_entry_close($zip_entry);
                    fclose($fp);
                }
            }
            zip_close($zip);
        }
    }
}

I call the method like so:


if(isset($_POST['add']) && $_POST['add'] == 'Upload'):
    $root = "../view/documents/";
    $target_path = $root.basename( $_FILES['ZipFile']['name']);

    if(isset($_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
        Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
    endif;
endif;

And finally my input element is like so:


<input type="file" name="ZipFile" id="ZipFile" value=""/>

But i get the following error when i try to upload:

Warning: zip_read() expects parameter 1 to be resource, integer given in public_html/library/Zip.class.php on line 11

Warning: zip_close() expects parameter 1 to be resource, integer given in public_html/library/Zip.class.php on line 22

Any ideas what i am doing wrong, i just want to be able to extract the Zip file int a folder called “documents”…

Am i missing something?

make sure you have the correct value for $filename

and instead of


if ($zip) {

do it like


if (is_resource($zip)) {

because it returns the number of error if filename does not exist :slight_smile:

from where the $filename you are reading from and not where do you want to extract it to - try it with it’s absolute path

and try to upload it on a temp folder first

are you sure your $filename has a correct value :slight_smile:

if


if (is_resource($zip)) { 
            die($zip);
}

doesn’t fire up there is an error, try


if (is_resource($zip)) { 
//ok
}
else{
            echo 'my ' . $filename . ' is incorrect';
            die($zip);
}

Hey,

Thanks, i don’t get the error now but it does not seem to be going into the IF statement.

I tried putting a die($zip); like so:


        if (is_resource($zip)) { 
            die($zip);
.
..
..

But it doesn’t seem to firing, so it doesn’t recognize this line i presume:


$zip = zip_open($filename);

I call the method like this:


    if(isset($_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
        Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
    endif;

I think i maybe doing this wrong?

no :slight_smile:

make sure your calling Report.zip from it’s correct path

:confused:

Ok this is weird…

i tried doing this:


if(isset($_POST['add']) && $_POST['add'] == 'Upload'):
    $root = "../view/documents/";
    $target_path = $root.basename( $_FILES['ZipFile']['name']);
    die($target_path);
    if(isset($_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
        Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
    endif;
endif;

die($target_path) gives me this:

…/view/documents/Report.zip

Which is correct, that is where i want to upload and extract the ZIP file to?

:frowning:

Thanks,

I got it working :wink:

Ok i tried that and i got this:

my Report.zip is incorrect

Is this correct?