PHP reading zip file (delete file, extract file, add file) example_PHP tutorial

WBOY
Release: 2016-07-13 10:48:30
Original
886 people have browsed it

The editor will show you several examples of operating zip files in PHP. We can read and delete the specified files in the zip package. Let’s introduce them below.

Extract files from zip archive

Delete files from a zip archive
The code is as follows
 代码如下 复制代码

/*
php 从zip压缩文件中提取文件
*/
$zip = new ZipArchive;

if ($zip->open('jQuery五屏上下滚动焦点图代码.zip') === TRUE) {//中文文件名要使用ANSI编码的文件格式
    $zip->extractTo('foldername');//提取全部文件
    //$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));//提取部分文件
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

Copy code

 代码如下 复制代码

/*
php 从一个zip压缩文件中删除文件
*/
$zip = new ZipArchive;
if ($zip->open('ajaxupload.zip') === TRUE) {
    $zip->deleteName('file.txt');//删除文件
    $zip->deleteName('testDir/');//删除文件夹
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

/*
php extract files from zip archive
*/
$zip = new ZipArchive;

if ($zip->open('jQuery five-screen scrolling up and down focus image code.zip') === TRUE) {//Chinese file names must use ANSI encoded file format
$zip->extractTo('foldername');//Extract all files
//$zip->extractTo('/my/destination/dir/', array('pear_item.gif', 'testfromfile.php'));//Extract some files
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

 代码如下 复制代码

/*
php 添加一个文件到zip压缩文件中
*/
$zip = new ZipArchive;

if ($zip->open('ajaxupload.zip') === TRUE) {//ajaxupload.zip 是已经存在的zip文件,注意中文文件名要注意编码问题
    $zip->addFile('33.xml');//添加新的文件
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

The code is as follows Copy code

Add a file to the zip archive
The code is as follows Copy code
/*
php adds a file to a zip file
*/
$zip = new ZipArchive;

if ($zip->open('ajaxupload.zip') === TRUE) {//ajaxupload.zip is an existing zip file. Please pay attention to encoding issues in Chinese file names
$zip->addFile('33.xml');//Add new file
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
http://www.bkjia.com/PHPjc/632775.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632775.htmlTechArticleThe following editor will show you several examples of php operating zip files. We can read the specified values ​​in the zip package File and delete the specified files in the zip package. Let’s introduce it below. From zip...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template