【问题标题】:PHP zipped directory becomes cpgz file when extractedPHP 压缩目录解压后变成 cpgz 文件
【发布时间】:2014-10-23 23:27:12
【问题描述】:

我在发帖前检查了几个问题,包括:

在本地服务器上测试时,一切都按预期工作,但是当我们上传到实时站点时,下载的 zip 文件以 .cpgz 文件打开。

我们正在使用“Conferences”目录(即 Asset_Management)中的文件夹名称以 html 表单填充 SELECT 菜单(名为“Conference”)。

PHP 像这样连接成一个链接:

<a href="zip_folders.php?directtozip=' . $_POST["Conference"] . '">Download All As Zip</a>

我们的邮政编码设置了 zip 文件:

<?php
// WARNING
// This code should NOT be used as is. It is vulnerable to path traversal. https://www.owasp.org/index.php/Path_Traversal
// You should sanitize $_GET['directtozip']
// For tips to get started see https://stackoverflow.com/questions/4205141/preventing-directory-traversal-in-php-but-allowing-paths

//Get the directory to zip
$filename_no_ext= $_GET['directtozip'];

// we deliver a zip file
header("Content-Type: archive/zip");

// filename for the browser to save the zip file
header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");

// get a tmp name for the .zip
$tmp_zip = tempnam ("tmp", "tempname") . ".zip";

//change directory so the zip file doesnt have a tree structure in it.
chdir('user_uploads/'.$_GET['directtozip']);

// zip the stuff (dir and all in there) into the tmp_zip file
exec('zip '.$tmp_zip.' *');

// calc the length of the zip. it is needed for the progress bar of the browser
$filesize = filesize($tmp_zip);
header("Content-Length: $filesize");

// deliver the zip file
$fp = fopen("$tmp_zip","r");
echo fpassthru($fp);

// clean up the tmp zip file
unlink($tmp_zip);
?>

但是当我解压文件时,它变成了.cpgz 文件。

为什么这可以在我们的本地服务器而不是我们的远程托管站点上运行?

编辑:我认为exec() 命令可能不可移植,但我不确定如何替换它。

【问题讨论】:

    标签: php macos download directory zip


    【解决方案1】:

    检查您本地的zip 和服务器zip 是否相同:

    whereis zip
    file /usr/bin/zip
    zip -v
    

    您的服务器可能在 zip 上有一些别名(例如 tar -cjf)。

    您获得了cpgz 文件,因为您的存档程序无法解压缩文件并尝试将其打包成cpgz 的格式。当我不小心将 tar.gz 存档命名为 tar.bz2 或反之亦然时,我遇到了这样的问题。

    【讨论】:

      【解决方案2】:

      我们的服务器禁用了 execfpassthru 命令,因此实际上没有将任何内容写入 zip 文件。解压一个空的 zip 文件给了我 .cpgz 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多