简体   繁体   中英

Creating zip files with PCLZIP.LIB: invalid variable type error

Trying to make an auto directory lister with a download function for every subdirectory. I'd like to make these subdirectories available as .zip files to allow for easier downloading and to use less bandwith on the server.

The following script was working fine until I added the pclzip.lib and tried to let it create a zip file.

<html>
<head>
<?php require_once("pclzip.lib.php");?>
<?php require_once("filesize_lib.php"); ?>

<style type="text/css">

.readme{
width:500px;
height:150px;
background: silver;
overflow-x: hidden;
overflow-y: scroll;
}


</style>

</head>
<body>
<?php
    $readme = "/readme.txt";
    $download = "downloads";
  // Openen
    $dir = new DirectoryIterator('.');
  // Doorlopen
?>
<table width="960px" border="1px"><tr><td width="185px"><strong>Name</strong></td><td width="50px"><strong>Type</strong></td><td width="50px"><strong>Size</strong></td><td width="125px"><strong>Last Modified</strong></td><td><strong>Short description</strong></td><td width="50px"><strong>Download</strong></td></tr>
<?php
    foreach ($dir as $file)
      {
        if (! $file->isDot()
               && $file != ".."
            && $file != "index.php"
            && $file != "filesize_lib.php"
            && $file != "downloads"
            )
        {        ?><tr><td><?php
                  echo '<a href="'.$file.'">'.$file.'</a>';
                ?></td><td><?php
                echo filetype($file);
                  ?></td><td><?php
                  echo fileORdirSize($file).'<br/>';
                 ?></td><td><?php
                  echo date("M j, Y", filemtime($file));    
                  ?></td><?php
                      if (filetype($file) == "dir"){
                          ?>                  
                          <td><div class="readme"><?php
                          echo file_get_contents($file.$readme);    
                          ?></div></td><?php
                      } else {
                          ?><td>Files don't have descriptions, but can be tested directly from this page.</td><?php
                      }
                  ?><td><?php
                    $zip = new PclZip("tmp/archief.zip");
                        if($zip->create($file) == 0)
                          die("Error : " . $zip->errorInfo(true));                
                  echo '<a href="'.$zip.'">'.$zip.'</a>';
                  ?></td></tr><?php
        }
  }
?>
</table>
</body>
</html> 

The error I'm receiving is the following:

Invalid variable type p_filelist [code -3]

Which I believe is due to the fact that I'm feeding pclzip.lib a single variable and not an array. Unfortunately, I don't know how to solve this problem. See the piece of code that is responsible for the problem (according to me) below:

<?php    // ----- Init
    $v_string_list = array();
    $v_att_list = array();
    $v_filedescr_list = array();
    $p_result_list = array();

    // ----- Look if the $p_filelist is really an array
    if (is_array($p_filelist)) {

      // ----- Look if the first element is also an array
      //       This will mean that this is a file description entry
      if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
        $v_att_list = $p_filelist;
      }

      // ----- The list is a list of string names
      else {
        $v_string_list = $p_filelist;
      }
    }

    // ----- Look if the $p_filelist is a string
    else if (is_string($p_filelist)) {
      // ----- Create a list from the string
      $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
    }

    // ----- Invalid variable type for $p_filelist
    else {
      PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
      return 0;
    }?>

Try instantiating your zip class before the start of the loop, and using the add() method rather than the create() method, otherwise you'll just be overwriting each archive with the next.

If you want to add a whole series of files in a single call, then either method expects an array of files, or a comma-separated list of files.

Ensure that $file is cast to string before calling the create() or add() method, you're passing an object

Also ensure that you're passing $file including any directory reference, else PCLZip won't find the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM