tags:

views:

24

answers:

1

hi i am working with php5 in this i upload a zip file and also extract the zip file in folder where i uploaded it. but problem is created when i want to read that file using zip_read function but its give following error.

Warning: zip_read() expects parameter 1 to be resource, integer given in C:\wamp\www\praveen\zipupload.php on line 28

so plz help me to resolve out from this problem. my code is following....

<?php

if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }

    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }

    $target_path = "upload/".$filename;  // change this to the correct site path
    if(move_uploaded_file($source, $target_path)) {
        $zip = new ZipArchive();


        $x = zip_open($target_path);
        print_r(zip_read($x)); exit;

            while($zipFile = zip_read($x))
            {
                echo "Filename: " . zip_entry_name($zipFile) . "<br>"; exit;
            }

        if ($x === true) {


            $zip->extractTo("upload/"); // change this to the correct site path

            $zip->close();

            unlink($target_path);
            $message = "Your .zip file was uploaded and unpacked.";
        }

    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>   
A: 

From the manual:

zip_open returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error.

So looks like your zip_open failed. You need to do an error check like:

$x = zip_open($target_path);
if (is_resource($x)) {
  // open succeeded.
}else{
  // open failed.
}
codaddict
thanksi do this it move in else part so what i have to do
Praveen kalal
any solution for this problem?
Praveen kalal
Make sure the `$target_path` is correct. Also ensure the zip file exists.
codaddict
yes its correct path. and also zip file is exists
Praveen kalal
Also check for permissions.
codaddict
which type of permissions?
Praveen kalal
thanks for your interaction.can you tell me which type of permission i have to check for this problem.
Praveen kalal
Check if your script has read permission on the file.
codaddict