【问题标题】:Zip inside zip (php)拉链内拉链 (php)
【发布时间】:2015-09-11 23:06:49
【问题描述】:

大家好,这几天我一直在尝试解决一个问题而没有任何回应,我不擅长 php,但我正在尽我所能。

我想自动下载,而不是在服务器上保存一个名为 Bigzip 的 zip

在这个 Bigzip 中:-另一个名为 Smallzip 的 zip

但打开下载的 zip 时出现错误,它已损坏

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();




//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';




zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path)
{


//create the file and throw the error if unsuccessful
if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$BzipN>\n");
}
if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$SzipN>\n");
}



//Add Smallzip to BigZip
$Bzip->addFile($file_path.$SzipN,$Szip);


$Szip->close();
$Bzip->close();
//then send the headers to foce download the Big zip file
header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$BzipN");
header("Content-length: " . filesize($BzipN));
header("Pragma: no-cache"); 
header("Expires: 0"); 
readfile("$BzipN");
exit;
}
?>

如果您有任何替代方案、想法、建议,我将很乐意接受。

谢谢

【问题讨论】:

  • 确保 htaccess 没有干扰:SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip)$ no-gzip dont-vary
  • @ScottMcGready 他的代码简直是一团糟。

标签: php download zip server


【解决方案1】:

问题编号 1:
您不能在空的 zip 档案中创建空的 zip 档案。这会导致文件损坏。

问题编号 2:
当您甚至还没有关闭第一个 zip 存档时,不要尝试将 zip 存档添加到另一个 zip 存档。

问题编号 3:
$Bzip-&gt;addFile($file_path.$SzipN,$Szip); 那么您究竟为什么要尝试将对象设置为您的文件名? => $Szip = new ZipArchive();

解决方案:

<?php

//Big and Small Archives names
$BzipN='Bigzip.zip';
$SzipN='Smallzip.zip';

//Big and Small Archives
$Bzip = new ZipArchive();
$Szip = new ZipArchive();

//File path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/PF/';


function zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path){
    // Create the file Smallzip.zip and throw the error if unsuccessful
    if ($Szip->open($SzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$SzipN>\n");
    }

    // Add a txt file to Smallzip so it isn't empty
    $Szip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n");

    // Close Smallzip.zip as we're done with it
    $Szip->close();

    // Create the file Bigzip.zip and throw the error if unsuccessful
    if ($Bzip->open($BzipN, ZIPARCHIVE::CREATE )!==TRUE) {
        exit("cannot open <$BzipN>\n");
    }

    // Add Smallzip.zip to Bigzip.zip with a valid name
    $Bzip->addFile($file_path.$SzipN,$SzipN);

    // Close Bigzip.zip as we're done with it
    $Bzip->close();

    //then send the headers to foce download the Big zip file
    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$BzipN");
    header("Content-length: " . filesize($BzipN));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$BzipN");

    // Delete the files from the server, even if the user cancels the download
    ignore_user_abort(true);
    unlink($file_path.$SzipN);
    unlink($file_path.$SzipN);
    exit;
}

zipFilesAndDownload($BzipN,$SzipN,$Bzip,$Szip,$file_path);

?>

【讨论】:

  • 非常感谢您的回答,非常感谢。另一方面,它将文件保存在服务器上,对吧?
  • 因为我不想保存在服务器上
  • @KPXD 没问题。这一切都是关于仔细调试你的代码并注意到任何奇怪的东西,再加上一些逻辑思考。是的,它将文件保存在服务器上,因为您正在创建文件。没有办法解决这个问题。但是您可以在之后轻松删除文件
  • 非常感谢。还有一个问题,我可以用 ($Szip->setPassword("MySecretPassword") 对 Szip 输入密码吗?
  • @KPXD 为什么不花一点力气去发现呢?您正在浪费您和我的时间在基本问题上,您可以通过简单的谷歌搜索或使用 php.net 轻松找到。 - 这不是不友好的,而是一种温和的推动,告诉你有时付出努力有它自己的回报。
【解决方案2】:

如果你不想在服务器上保存文件,只需回显创建的文件。 index.php

<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>

test.php

<?php
$filename = 'test-download.zip';
$htmlcode1 = "<HTML> \n <BODY>";
$htmlcode2 = "</BODY> \n <HTML>";
$somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2;
!$handle = fopen($filename, 'w');
fwrite($handle, $somecontent);
fclose($handle);


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

?>

【讨论】:

  • 那么,这与 zip 存档以及他的 zip 存档损坏的原因到底有什么关系?
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多