Home  >  Article  >  Backend Development  >  How to create zip file using PHP

How to create zip file using PHP

青灯夜游
青灯夜游Original
2019-02-15 10:57:268015browse

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.

How to create zip file using PHP

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(&#39;test_new.zip&#39;, ZipArchive::CREATE) === TRUE)
{
    // 将文件添加到zip文件
    $zip->addFile(&#39;test.txt&#39;);
    $zip->addFile(&#39;test.pdf&#39;);
 
    // 将random.txt文件添加到zip并将其重命名为newfile.txt
    $zip->addFile(&#39;random.txt&#39;, &#39;newfile.txt&#39;);
 
    // 将有指定文本的new.txt文件添加到zip文件中
    $zip->addFromString(&#39;new.txt&#39;, &#39;要添加到new.txt文件中的文本&#39;);
 
    // 关闭zip文件
    $zip->close();
}
?>

How to create zip file using PHP

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(&#39;test_overwrite.zip&#39;, ZipArchive::OVERWRITE) === TRUE)
{
    // 将文件添加到zip文件
    $zip->addFile(&#39;test.txt&#39;);
    $zip->addFile(&#39;test.pdf&#39;);
 
    // 关闭zip文件
    $zip->close();
}
?>

How to create zip file using PHP

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(&#39;test_folder.zip&#39;, ZipArchive::CREATE) === TRUE)
{
    // 将文件添加到zip文件中的demo_folder文件夹内
    $zip->addFile(&#39;text.txt&#39;, &#39;demo_folder/test.txt&#39;);
    $zip->addFile(&#39;test.pdf&#39;, &#39;demo_folder/test.pdf&#39;);
 
    // 将random.txt文件添加到zip文件中的demo_folder文件夹内,并重命名为newfile.txt
    $zip->addFile(&#39;random.txt&#39;, &#39;demo_folder/newfile.txt&#39;);
 
    //  将有指定内容的new.txt添加到zip文件中的demo_folder文件夹
    $zip->addFromString(&#39;demo_folder/new.txt&#39;, &#39;要添加到new.txt文件中的文本&#39;);
 
    // 关闭zip文件
    $zip->close();
}
?>

How to create zip file using PHP

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(&#39;test_folder_change.zip&#39;, ZipArchive::CREATE) === TRUE)
{
    // 将文件添加到zip文件
    $zip->addFile(&#39;text.txt&#39;, &#39;demo_folder/test.txt&#39;);
    $zip->addFile(&#39;test.pdf&#39;, &#39;demo_folder1/test.pdf&#39;);
 
    // 关闭zip文件
    $zip->close();
}
?>

How to create zip file using PHP

5-How to create zip file using PHP

5-How to create zip file using PHP

##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 folder

Create a zip file containing all files in a directory

<?php
$zip = new ZipArchive;
if ($zip->open(&#39;test_dir.zip&#39;, ZipArchive::OVERWRITE) === TRUE)
{
    if ($handle = opendir(&#39;demo_folder&#39;))
    {
        // 添加目录中的所有文件
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != ".." && !is_dir(&#39;demo_folder/&#39; . $entry))
            {
                $zip->addFile(&#39;demo_folder/&#39; . $entry);
            }
        }
        closedir($handle);
    }
 
    $zip->close();
}
?>

How to create zip file using PHP

Code Description

Chapter 5- Line 16: Open a directory and create a zip file containing all the files in the directory

Line 5: Open the directory

Line 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 directory

Line 18: Close zip file

Create 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(&#39;test_files_dirs.zip&#39;, ZipArchive::OVERWRITE) === TRUE)
{
    // 添加 directory1
    if ($handle = opendir(&#39;demo_folder/directory1/&#39;))
    {
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != "..")
            {
                $zip->addFile(&#39;demo_folder/directory1/&#39; . $entry);
            }
        }
        closedir($handle);
    }
 
    // 添加 directory2
    if ($handle = opendir(&#39;demo_folder/directory2/&#39;))
    {
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != "..")
            {
                $zip->addFile(&#39;demo_folder/directory2/&#39; . $entry);
            }
        }
        closedir($handle);
    }
 
    // 添加 directory3
    if ($handle = opendir(&#39;demo_folder/directory3/&#39;))
    {
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != "..")
            {
                $zip->addFile(&#39;demo_folder/directory3/&#39; . $entry);
            }
        }
        closedir($handle);
    }
    $zip->close();
}
?>

How to create zip file using PHP

7-How to create zip file using PHP

7-How to create zip file using PHP

7-How to create zip file using PHP

##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!

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