Archived Forum Post

Index of archived forum posts

Question:

PHP Zip to memory then provide as download to user..

Oct 03 '12 at 23:59

hi, Greetings every one..

in my project/script, i need to create a zip file and write zip to memory (instead of file) using WriteToMemory function..

but the problem is i can't save the memory zip date to file :(

here is the related code:

$success = $objZip->WriteToMemory($objByte);
if ($success != true)
{
    exit('Zip Write error!');
}

$strZipData =  $objByte->to_s();
$intZipSize = $objByte->getSize();
header('Content-Disposition: attachment; filename="Sample.zip"');
//header("Content-type: application/zip";
header('Content-Transfer-Encoding: binary');
header("Content-length: " . $intZipSize);  
echo $strZipData;

but it only write a 5bytes string file :( and in that file it contains only:

PK

i think i have give enough information for you guys to find out the issue..

so, any help would be appreciated :)

best regards


Accepted Answer

Thanks mAlam,

Here are new builds that should work now:

(all for Linux 64-bit)

PHP v5.2.17: http://www.chilkatsoft.com/preRelease/chilkat-9.3.2-php-5.2.17-x86_64-linux.tar.gz

PHP v5.3.*: http://www.chilkatsoft.com/preRelease/chilkat-9.3.2-php-5.3-x86_64-linux.tar.gz

PHP v5.4.*: http://www.chilkatsoft.com/preRelease/chilkat-9.3.2-php-5.4-x86_64-linux.tar.gz

Your original code using "$objByte->to_s()" should work. You may also use either "$objByte->getData()" or "$objByte->getBytes()", all of which return the data in exactly the same way.

PHP is one of the only languages where the string primitive type is really just a sequence of bytes, not necessarily null terminated, and may even contain null bytes within. Normally, one would never handle binary data as if it were a "string" because in virtually all other programming languages, the difference between a "string" and "binary data" is that it's implied that the bytes in a string are interpreted according to a character encoding, such as utf-8, utf-16, iso-8859-1, etc.


Answer

While investigating this question, I noticed that the PHP Extension documentation for CkString and CkByteData was missing, so I updated the online reference documentation:

http://www.chilkatsoft.com/refdoc/phpCkByteDataRef.html

and

http://www.chilkatsoft.com/refdoc/phpExtCkStringRef.html

The $objByte->to_s() method is going to interpret the bytes according to the ANSI character encoding, so it's not something that should be called if the bytes do not represent actual character data, which is the case here.

Instead, try calling $objByte->getBytes() to return the unmodified bytes.


Answer

hello? any update please?


Answer

Please let me know what operating system and whether it's 32-bit or 64-bit so I can provide a new build for testing..


Answer

I tested with the following PHP script, and the .zip file is correctly written:

$objByte = new CkByteData();
$success = $zip->WriteToMemory($objByte);
if ($success != true) {
    print $zip->lastErrorText() . "n";
    exit;
}
$zipFileImage = $objByte->getData();
$fp = fopen("out.zip","w");
fwrite($fp,$zipFileImage);

Therefore, I know that $objByte->getData() is returning the correct bytes.


Answer

once again.. thanks a lot for your reply

sir, i did gave you my full source code (php).. and i don't think there is any issue in my code..

so, would you please recheck my code.. and once again.. i don't want. to save the zipImage to file, i need to provide to user as a download (attachment header)

so, i uploaded my code again, and i think it will also run on your server too..so, would you please check again..

PHP ZIP Source

best regards


Answer

hello? sir? i m still waiting.. for your kind reply..

would you please update me .. and give me a solution.. .. :) ? please?

best regards


Answer

I have shown that the new build emits the exact bytes of the zip archive produced by WriteToMemory method. Here it is:

$objByte = new CkByteData();
$success = $zip->WriteToMemory($objByte);
if ($success != true) {
    print $zip->lastErrorText() . "n";
    exit;
}
$zipFileImage = $objByte->getData();
$fp = fopen("out.zip","w");
fwrite($fp,$zipFileImage);

If you test this code in a simple PHP script, you can verify that it produces a file that is a valid zip archive.

Given that this works, the only remaining task is to form a valid HTTP response. HTTP responses are MIME, so you want to send a MIME header that correctly specifies the content, and then emit the bytes of the zip archive. The PHP task of sending the HTTP response only involves Chilkat for the emitting of the binary Zip archive bytes. I've proven that $objByte->getData() emits the bytes correctly. The task of emitting the correct HTTP response header via PHP is not a Chilkat Zip issue. You should be able to find more information on the web regarding this..


Answer

based on your reply, i have tried this:

file_put_contents('test.zip', $strZipData);

but php show me following error:

file_put_contents(): supplied resource is not a valid stream resource

so, i m really confused is really your object return valid zip data :(

and for your kind information, in my last post, i have attached my php script where mime header is still exits (few were commented), so, is that very hard for you to help me a bit.. by fixing the mime/header part?

please sir, leave a reply asap

best regards


Answer

and yes i just tried with following code too:

    $lfile = fopen('test.zip', 'w');
fwrite($lfile,$strZipData);
fclose($lfile);

but again php return following error:

fwrite() expects parameter 2 to be string

hope that may help you.. :)

best regards