Home  >  Article  >  Backend Development  >  Use of php ZipArchive class

Use of php ZipArchive class

WBOY
WBOYOriginal
2016-10-22 00:14:131118browse

The thing is like this: Prepare to do a batch download, put all the resources that need to be downloaded in a compressed package, and use the ZipArchive class

Code:

<code>$filename='test.zip';
$zip=new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);
$zip->addFile('favicon.ico');
$zip->close();
var_dump(fopen($filename,'r'));
var_dump(filesize($filename));
</code>

But here comes the problem
1. You must add files to the zip before it can be opened correctly. My personal understanding is that if the zip is empty, the system will not create the zip. In fact, it is not created. Is that so?
2. My test is to fopen or filesize after $zip->close(). The parameter is $filename. Why?
3. After testing, the parameter of fopen or filesize cannot be $zip->filename. Why?

Reply content:

The thing is like this: prepare to do a batch download, put all the resources that need to be downloaded in a compressed package, and use the ZipArchive class

Code:

<code>$filename='test.zip';
$zip=new ZipArchive();
$zip->open($filename,ZipArchive::CREATE);
$zip->addFile('favicon.ico');
$zip->close();
var_dump(fopen($filename,'r'));
var_dump(filesize($filename));
</code>

But here comes the problem
1. You must add files to the zip before it can be opened correctly. My personal understanding is that if the zip is empty, the system will not create the zip. In fact, it is not created. Is that so?
2. My test is to fopen or filesize after $zip->close(). The parameter is $filename. Why?
3. After testing, the parameter of fopen or filesize cannot be $zip->filename. Why?

Before executing $zip->addFile, first determine whether the file to be compressed exists.
After $zip->close, $zip can no longer be used, so $zip->filename cannot be used.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Image upload problemNext article:Image upload problem