首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zip内部zip (php)

Zip内部zip (php)
EN

Stack Overflow用户
提问于 2015-09-11 23:06:49
回答 2查看 505关注 0票数 1

大家好,已经过了几天了,我正试图在没有任何反应的情况下解决一个问题,我并不擅长php,但我正在尽我最大的努力。

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

在这个Bigzip里面:名为Smallzip的-Another zip

但打开下载的zip时出错,它是损坏的

代码语言:javascript
复制
<?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;
}
?>

如果你有其他的选择,想法,建议,我很乐意接受。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-11 23:55:07

第1期:

您不能在空压缩档案中创建空压缩档案。会导致文件损坏。

第2期:

不要尝试将zip归档添加到另一个zip归档中,而您甚至还没有关闭第一个压缩归档。

第3期:

$Bzip->addFile($file_path.$SzipN,$Szip);,那么为什么要将对象设置为文件名呢?=> $Szip = new ZipArchive();

解决方案:

代码语言:javascript
复制
<?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);

?>
票数 2
EN

Stack Overflow用户

发布于 2015-09-11 23:54:52

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

代码语言:javascript
复制
<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>

test.php

代码语言:javascript
复制
<?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);

?>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32533331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档