【问题标题】:Zipfile created with ZipArchive unzips to cpgz使用 ZipArchive 创建的 Zipfile 解压缩到 cpgz
【发布时间】:2016-01-27 11:52:59
【问题描述】:

我有这个页面,您可以在其中将多个文件下载到 zip。在我的代码中这工作正常,但是当我尝试解压缩它时,它会解压缩到 filename.zip.cpgz。

这可能与我使用 WordPress 的事实有关,因为我之前在没有它的情况下尝试过,然后它运行良好......

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

警告:filesize(): stat failed for unstraight.zip in /Users/johannaskogh/Desktop/workspace/olivia_schough/wp-content/themes/schoughs/page-research.php 16 行

警告:无法修改标头信息 - 标头已发送(输出开始于 /Users/johannaskogh/Desktop/workspace /olivia_schough/wp-content/themes/schoughs/page-research.php:16) 在 /Users/johannaskogh/Desktop/workspace/olivia_schough/wp-content/themes/schoughs/page-research.php16 行

警告:readfile(unstraight.zip):无法打开流:/Users/ 中没有这样的文件或目录johannaskogh/Desktop/workspace/olivia_schough/wp-content/themes/schoughs/page-research.php18

警告: unlink(unstraight.zip):在 /Users/johannaskogh/Desktop/workspace/olivia_schough/wp-content/themes/schoughs/page-research.php19 中没有这样的文件或目录

这是处理 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-&gt;getFilename(),

$file-&gt;getRealPath();

&lt;input type="checkbox" name="files[]" value="&lt;?php echo $file-&gt;getFilename(); ?&gt;"&gt;

然后 zip 可以按应有的方式解压缩,但这里的问题是我得到了我不想要的文件的整个路径。我只想要文件。

【问题讨论】:

    标签: php zip ziparchive


    【解决方案1】:

    好的,所以我设法解决了问题,即我没有文件夹的路径。

    我把表单中的value改成了$file-&gt;getRealPath(); ?&gt;"&gt;

    为了确保文件的整个路径在 zip 文件中没有出现,我添加了 $zip-&gt;addFile($file, basename($file));

    【讨论】:

      猜你喜欢
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多