邮政编码(php) [英] Zip inside zip (php)

查看:171
本文介绍了邮政编码(php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,几天之内,我试图解决一个问题,没有任何回应,我不是很出色的php,但我正在尽力。

Hi everyone it's been a couple of days I'm trying to solve a problem without having any response, I'm not excelling at php but I am doing my best.

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

I want to put automatially for download, and without saving on the server a zip called Bigzip

在这个Bigzip中:另一个zip文件叫Smallzip

Inside this Bigzip : -Another zip called Smallzip

但是,打开已下载的zip时,我收到错误,已损坏

But I get error when opening the downloaded zip, it is corrupted

<?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;
}
?>

如果您有任何替代方案,想法和建议,我将会冒险采取。

If you have any alternatives, ideas, suggestions I will gladely take it.

谢谢

推荐答案

发行号码1:
您无法在空的zip档案中创建空的zip档案。这将导致文件损坏。

Issue number 1:
You can't create empty zip archives in empty zip archives. That'll result in a corrupt file.

发行号码2:

请勿尝试添加zip文件存档到另一个zip存档,而您尚未关闭第一个zip存档。

Issue number 2:
Don't try to add a zip archive to another zip archive while you haven't even closed the first one yet.

发行号码3:

$ Bzip-> addFile($ file_path。$ SzipN,$ Szip); 那么为什么你试图将对象设置为你的文件名? => $ Szip = new ZipArchive();

Issue number 3:
$Bzip->addFile($file_path.$SzipN,$Szip); So why exactly are you trying to set the object as your filename? => $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);

?>

这篇关于邮政编码(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆