简体   繁体   English

PHP:$ zip-> addFile问题

[英]PHP: $zip->addFile issues

I have written a simple script to add files to an archive. 我编写了一个简单的脚本,将文件添加到存档中。 After much head scratching I can't get the script to work. 经过大量的努力,我无法使脚本正常工作。

I have a php file here which reads from a check-box, the file/s selected in the check-box are added to the array $file. 我这里有一个php文件,它从一个复选框读取,在复选框中选择的文件被添加到数组$ file中。

$path = ('a path');
$dir = scandir('another path');

    if(isset($_POST["submit"])){
    if(!isset($_POST["File"])){
        echo 'A file must be selected to archive it';
        } else { 
            require_once('zip_function.php');
            $file = $_POST['File'];
            $goZipper = goZipper($file, $path);
            if($goZipper == false) {
                echo ("Error");
            } else { 
                echo ("Success");
            }
        }   
    }

The function goZipper is called and $file and the destination are passed to it, the function is below. 调用函数goZipper并将$ file和目标传递给它,该函数在下面。

function goZipper($files, $destination = '',$overwrite = true) {
    if(file_exists($destination) && !$overwrite) {
        echo"File exists";
        return false; 
        }
    if(count($files)){
        $zip = new ZipArchive();
            if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true){
                echo"Error opening destination";
                return false;
            } 
            foreach($files as $file){
                $zip->addFile($file,basename($file));

                    echo("<pre>");
                    var_dump($zip);
                    exit();

            }
            $zip->close();
            return file_exists($destination);
            }
            else{
                return false;
            }
    }

The function is returning true every time it is called. 该函数在每次调用时都返回true。 But no files are being added. 但是没有文件被添加。 Can anyone spot an obvious error here? 有人可以在这里发现明显的错误吗?

ZipArchive::addFile() expects the first param to be a string containing the filename. ZipArchive :: addFile()期望第一个参数是包含文件名的字符串。 But you are passing a value of $_POST['File'] to it which is an array as $_POST['File'] is a two dimensional array. 但是您将$ _POST ['File']的值传递给它,这是一个数组,因为$ _POST ['File']是一个二维数组。
See here for the contents of $_POST['File']. 有关$ _POST ['File']的内容,请参见此处。

What you need to do is change this line: 您需要做的是更改此行:

$zip->addFile($file,basename($file));

To: 至:

$zip->addFile($file['tmp_name'],$file['name']);

Zip::addFile needs absolute path in the first parameter, and the second parameter would be file name as mention in PhpDOc Zip :: addFile在第一个参数中需要绝对路径,第二个参数将是文件名,如PhpDOc中所述

$zip->addFile('/path/to/index.txt', 'newname.txt');

And make sure you are getting $file variable with absolute path. 并确保获得带有绝对路径的$file变量。 if you are uploading file from browser then you should use $_FILE['file']['tmp_name'] in the $zip->addFile method on first parameter 如果要从浏览器上传文件,则应在第一个参数的$zip->addFile方法中使用$_FILE['file']['tmp_name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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