1

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.

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

Inside this Bigzip : -Another zip called Smallzip

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.

Thanks

KPXD
  • 53
  • 8

2 Answers2

2

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

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.

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();

Solution:

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

?>
icecub
  • 8,615
  • 6
  • 41
  • 70
  • Thanks a lot for your answer, I really appreciate-it. In the other hand it saves files on server, right ? – KPXD Sep 12 '15 at 01:11
  • Because I don't want to save on the server – KPXD Sep 12 '15 at 01:11
  • @KPXD No problem. It's all about carefully debugging your code and noticing anything strange, plus some logical thinking. Ye it saves the file on the server because you're creating the file. There's no way around that. But you can easily just delete the file afterwards – icecub Sep 12 '15 at 01:14
  • Thanks a lot. One more question, can I put to Szip a password with ($Szip->setPassword("MySecretPassword") right ? – KPXD Sep 12 '15 at 01:44
  • @KPXD Why don't put a small efford in it and find out? You are wasting both yours and my time with basic questions you can easily find out with simpel google searches or using php.net. - This is not ment as unfriendly, but a gentle push to teach you that sometimes putting efford in something has it's own rewards. – icecub Sep 12 '15 at 04:59
-1

If u dont want to save file on server just echo created file . 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);

?>
vivek
  • 1
  • 1
  • 1
    So, what exactly does this have to do with zip archives and the reason his zip archive was corrupted? – icecub Sep 12 '15 at 00:01