Re: [PHP] create archive file in memory with zipArchive class

[email protected] (Richard Quadling)
Newsgroups php.general
Message-ID <[email protected]>
On 9 February 2010 15:42, Ryan Sun <[email protected]> wrote:
> I want to generate credential zip file for user on the fly with
> zipArchive and render it for download, so I created following code
> ---------------------------------------------------------
> $zip = new ZipArchive();
> $filename = '/tmp/xxx.zip';
> if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
>      throw new Exception();
> }
> if($zip)
> {
>     $zip->addFromString('xxx.xx', $fileString);
> }
> $zip->close();
> $fileString = file_get_contents($filename);
> unlink($filename);
>
> $this->getResponse()->setHeader('Content-Type', 'application/zip');
> $this->getResponse()->setHeader('Content-Disposition','attachment;filename=xxx.zip');
> $this->getResponse()->setBody($fileString);
> -----------------------------------------------------
> it works, but I think creating the file in memory is a better
> approach, so I changed the 2nd lineI(using php 5.2.0) to
> $filename = 'php://temp/xxx.zip';
> then the php just won't archive the file and the file downloaded is
> just a plain text file.
>
> so
> question 1, how to create zip Archive file in memory on the fly and
> download it (I don't have to save it on disk)?
> question 2, if there is no way to create in memory, is it safe to just
> unlink() the file?
>
> thanks in advance.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

According to http://docs.php.net/manual/en/wrappers.php.php, it looks
like you should be using ...

$filename = 'php://temp';

That's it.

The actual file name is not your job.



-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.