create archive file in memory with zipArchive class
create archive file in memory with zipArchive class
am 09.02.2010 16:42:45 von Ryan Sun
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 =3D new ZipArchive();
$filename =3D '/tmp/xxx.zip';
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
throw new Exception();
}
if($zip)
{
=A0 $zip->addFromString('xxx.xx', $fileString);
}
$zip->close();
$fileString =3D file_get_contents($filename);
unlink($filename);
$this->getResponse()->setHeader('Content-Type', 'application/zip');
$this->getResponse()->setHeader('Content-Disposition','attac hment;filename=
=3Dxxx.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 =3D '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
Re: create archive file in memory with zipArchive class
am 09.02.2010 17:13:08 von Richard Quadling
On 9 February 2010 15:42, Ryan Sun 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 =3D new ZipArchive();
> $filename =3D '/tmp/xxx.zip';
> if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
> Â Â Â Â throw new Exception();
> }
> if($zip)
> {
> Â Â Â $zip->addFromString('xxx.xx', $fileString);
> }
> $zip->close();
> $fileString =3D file_get_contents($filename);
> unlink($filename);
>
> $this->getResponse()->setHeader('Content-Type', 'application/zip');
> $this->getResponse()->setHeader('Content-Disposition','attac hment;filenam=
e=3Dxxx.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 =3D '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 =3D 'php://temp';
That's it.
The actual file name is not your job.
--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: create archive file in memory with zipArchive class
am 09.02.2010 17:47:01 von Ryan Sun
thanks, Richard, maybe you are right, the actual file name is not my job
I changed it to 'php://temp' but its still the same, nothing has been chang=
ed...
On Tue, Feb 9, 2010 at 11:13 AM, Richard Quadling
wrote:
> On 9 February 2010 15:42, Ryan Sun 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 =3D new ZipArchive();
>> $filename =3D '/tmp/xxx.zip';
>> if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
>> throw new Exception();
>> }
>> if($zip)
>> {
>> =A0 $zip->addFromString('xxx.xx', $fileString);
>> }
>> $zip->close();
>> $fileString =3D file_get_contents($filename);
>> unlink($filename);
>>
>> $this->getResponse()->setHeader('Content-Type', 'application/zip');
>> $this->getResponse()->setHeader('Content-Disposition','attac hment;filena=
me=3Dxxx.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 =3D '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 =3D '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=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: create archive file in memory with zipArchive class
am 10.02.2010 12:48:48 von Richard Quadling
On 9 February 2010 16:47, Ryan Sun wrote:
> thanks, Richard, maybe you are right, the actual file name is not my job
> I changed it to 'php://temp' but its still the same, nothing has been cha=
nged...
>
> On Tue, Feb 9, 2010 at 11:13 AM, Richard Quadling
> wrote:
>> On 9 February 2010 15:42, Ryan Sun 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 =3D new ZipArchive();
>>> $filename =3D '/tmp/xxx.zip';
>>> if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
>>> Â Â Â Â throw new Exception();
>>> }
>>> if($zip)
>>> {
>>> Â Â Â $zip->addFromString('xxx.xx', $fileString);
>>> }
>>> $zip->close();
>>> $fileString =3D file_get_contents($filename);
>>> unlink($filename);
>>>
>>> $this->getResponse()->setHeader('Content-Type', 'application/zip');
>>> $this->getResponse()->setHeader('Content-Disposition','attac hment;filen=
ame=3Dxxx.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 =3D '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 =3D '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=3DZEND002498&r=3D213=
474731
>> ZOPA : http://uk.zopa.com/member/RQuadling
>>
>
It looks like this isn't possible.
--=20
-----
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=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php