Home > Article > Backend Development > How to create zip file using PHP
Using compressed files can save disk space; and compressed files are smaller, convenient for network transmission, and highly efficient. This article introduces how to use PHP to create zip compressed files. I hope it will be helpful to you.
In PHP there is a ZipArchive class that can be easily used to create zip files. The following is an example of how to create a zip file in PHP. [Video tutorial recommendation: PHP tutorial]
Create a new zip file
The following code will create a new zip file (test_new.zip) and add some files to it.
<?php $zip = new ZipArchive; if ($zip->open('test_new.zip', ZipArchive::CREATE) === TRUE) { // 将文件添加到zip文件 $zip->addFile('test.txt'); $zip->addFile('test.pdf'); // 将random.txt文件添加到zip并将其重命名为newfile.txt $zip->addFile('random.txt', 'newfile.txt'); // 将有指定文本的new.txt文件添加到zip文件中 $zip->addFromString('new.txt', '要添加到new.txt文件中的文本'); // 关闭zip文件 $zip->close(); } ?>
Code description:
Line 2: Create an object of ZipArchive class
Line 3: Used to create and open a file called test_new.zip so that we can add files to it. The flag ZipArchive::CREATE specifies that we want to create a new zip file
Lines 6 and 7: used to add files to the zip file.
Line 10: Used to add a file named random.txt to the zip file and rename it to newfile.txt in the zip file.
Line 13: Used to add a new file new.txt. The content of the file is "text to be added to the new.txt file".
Line 16: Close and save changes to the zip file.
Note: Sometimes problems can occur when using relative paths to files. If there is any problem using the path, then we can also use the absolute path of the file
Overwrite the existing zip file
If you want to overwrite the existing zip file For zip files, we can use code similar to the following. The flag ZipArchive::OVERWRITE specifies overwriting existing zip files.
<?php $zip = new ZipArchive; if ($zip->open('test_overwrite.zip', ZipArchive::OVERWRITE) === TRUE) { // 将文件添加到zip文件 $zip->addFile('test.txt'); $zip->addFile('test.pdf'); // 关闭zip文件 $zip->close(); } ?>
Code description
This code will create a file test_overwrite.zip, if the file already exists, the file will be overwritten by this new file.
Create a new zip file and add files in the specified folder
<?php $zip = new ZipArchive; if ($zip->open('test_folder.zip', ZipArchive::CREATE) === TRUE) { // 将文件添加到zip文件中的demo_folder文件夹内 $zip->addFile('text.txt', 'demo_folder/test.txt'); $zip->addFile('test.pdf', 'demo_folder/test.pdf'); // 将random.txt文件添加到zip文件中的demo_folder文件夹内,并重命名为newfile.txt $zip->addFile('random.txt', 'demo_folder/newfile.txt'); // 将有指定内容的new.txt添加到zip文件中的demo_folder文件夹 $zip->addFromString('demo_folder/new.txt', '要添加到new.txt文件中的文本'); // 关闭zip文件 $zip->close(); } ?>
Code description
The above code will add different files in the zip file to the demo_folder folder
The second parameter of the addfile function can be used to store the files in a new file folder
The first parameter in the addFromString function can be used to store the file in a new folder
Create a new zip file and add the file to a different In the folder
<?php $zip = new ZipArchive; if ($zip->open('test_folder_change.zip', ZipArchive::CREATE) === TRUE) { // 将文件添加到zip文件 $zip->addFile('text.txt', 'demo_folder/test.txt'); $zip->addFile('test.pdf', 'demo_folder1/test.pdf'); // 关闭zip文件 $zip->close(); } ?>
##Code Description
We store the test.txt file in the zip file into the demo_folder folder and the test.pdf file into the demo_folder1 folderCreate a zip file containing all files in a directory
<?php $zip = new ZipArchive; if ($zip->open('test_dir.zip', ZipArchive::OVERWRITE) === TRUE) { if ($handle = opendir('demo_folder')) { // 添加目录中的所有文件 while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != ".." && !is_dir('demo_folder/' . $entry)) { $zip->addFile('demo_folder/' . $entry); } } closedir($handle); } $zip->close(); } ?>
Code Description
Chapter 5- Line 16: Open a directory and create a zip file containing all the files in the directoryLine 5: Open the directoryLine 8: Get the name of each file in the directory Line 10: Skip "." and ".." and any other directories Line 12: Add files to the zip file Line 15: Close directoryLine 18: Close zip fileCreate a zip file containing all files from multiple directories
The following code adds the different folders and files in these directories to the zip file<?php $zip = new ZipArchive; if ($zip->open('test_files_dirs.zip', ZipArchive::OVERWRITE) === TRUE) { // 添加 directory1 if ($handle = opendir('demo_folder/directory1/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $zip->addFile('demo_folder/directory1/' . $entry); } } closedir($handle); } // 添加 directory2 if ($handle = opendir('demo_folder/directory2/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $zip->addFile('demo_folder/directory2/' . $entry); } } closedir($handle); } // 添加 directory3 if ($handle = opendir('demo_folder/directory3/')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $zip->addFile('demo_folder/directory3/' . $entry); } } closedir($handle); } $zip->close(); } ?>##Code description
Lines 6-42: Add all files in directories directory1, directory2 and directory3 to the corresponding files in the zip file in the directory.
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of How to create zip file using PHP. For more information, please follow other related articles on the PHP Chinese website!