提取时,PHP压缩目录成为cpgz文件

时间:2014-08-29 22:12:29

标签: php macos download directory zip

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

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

我们在html表单中填写了一个SELECT菜单(名为" 会议"),文件夹名称位于我们的" Conferences "目录(即Asset_Management)。

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()命令可能无法移植,但我不确定如何替换它。

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

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

相关问题