Php 使用ZipParchive创建的Zipfile解压到cpgz

Php 使用ZipParchive创建的Zipfile解压到cpgz,php,zip,ziparchive,Php,Zip,Ziparchive,我有这个页面,你可以下载多个文件到一个zip。在我的代码中,这很好,但当我试图解压缩它时,它会解压缩到filename.zip.cpgz 这可能与我使用WordPress有关,因为我以前在没有WordPress的情况下尝试过,然后效果很好 在文本编辑器中打开文件时,会收到以下错误消息: 警告:filesize():第16行/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page-res

我有这个页面,你可以下载多个文件到一个zip。在我的代码中,这很好,但当我试图解压缩它时,它会解压缩到filename.zip.cpgz

这可能与我使用WordPress有关,因为我以前在没有WordPress的情况下尝试过,然后效果很好

在文本编辑器中打开文件时,会收到以下错误消息:

警告:filesize():第16行/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page-research.php中unstaight.zip的stat失败

警告:无法修改标题信息-标题已由第16行/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page research.php中的/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page-research.php:16发送

警告:readfile(unstaight.zip):无法打开流:第18行的/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page-research.php中没有此类文件或目录

警告:取消链接(unstraight.zip):第19行的/Users/johannaskogh/Desktop/workspace/olivia_schough/wp content/themes/schoughs/page-research.php中没有此类文件或目录

下面是处理zip的代码,位于文件顶部:

<?php

$error = ""; //error holder
if(isset($_POST['createzip'])) {
    $files = $_POST['files'];
    $zipname = time() . ".zip"; // Zip name
    $zip = new \ZipArchive(); // Load zip library
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();
    // push to download the zip
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename=' . $zipname);
    header('Content-Length: ' . filesize($zipname));
    ob_clean();
    readfile($zipname);
    unlink($zipname);
}
?>

下面是它的其余部分:

<?php get_header(); ?>

<div class="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="article article--about theme">
    <div class="article__part--bottom">
        <div class="container">
            <h1 class="article__title"><?php the_title();?></h1>
            <div class="article__content">
            <?php the_content(); ?>
        </div>
    </div>            
    <div class="container">
        <div class="article__content">
            <form name="zips" method="post">

                <?php
                $upload_dir = wp_upload_dir();

                // path to directory
                $directory = $upload_dir['path'] . '/research/';
                $iterator = new \DirectoryIterator($directory);
                ?>

                <div class="checkbox-list">
                <?php foreach ($iterator as $file) {
                    if ($file->isFile()) {
                        ?>
                        <div class="checkboxes">
                            <input type="checkbox" name="files[]" value="<?php echo $file->getFilename(); ?>">
                            <h3 class="font-maxima">"<?php echo $file->getFilename(); ?>"</h3>
                            <span class="smaller-font">The Unstraight Museum</span>
                        </div>

                    <?php

                    }
                }
    <input type="submit" name="createzip" value="Download as ZIP">
</form>

<?php get_footer(); ?>


好的,所以我设法解决了这个问题,那个就是我并没有到文件夹的路径

我将表单中的
值更改为
$file->getRealPath();?>“>

为了确保文件的整个路径在zip文件中不协调,我添加了
$zip->addFile($file,basename($file));