Home > Backend Development > PHP Tutorial > PHP file compressed package

PHP file compressed package

不言
Release: 2023-03-25 17:04:01
Original
2173 people have browsed it

This article mainly introduces the compression package of PHP files, which has certain reference value. Now I share it with everyone. Friends in need can refer to

All files in the compressed directory

In order to facilitate future reuse, I have written three functions here, which must be placed together under a class. Readers can also separate them according to the situation, but please pay attention to changing the class name of the method call (self is used here).

Question: Here I want to compress all files (including folders) under the current path into a compressed package. I don’t want to compress extra folders. I don’t know how to do it. I hope someone with knowledge can tell me. Thank you!

   /**
     * add path to zip
     * Here, I want to add all the files under the current path, but I don't know how to add it, I want to get help, thank you!
     * @param $path
     * @param $zip
     */
    public static function addDirectoryToZip($path, $zip)
    {
        self::createPath([$path, $zip]);        
        $zipObj = new \ZipArchive();        
        if ($zipObj->open($zip, \ZipArchive::CREATE) === TRUE) {            
        $handler = opendir($path);            
        while (($filename = readdir($handler))) {                
        if ($filename != "." && $filename != "..") {                    
        $tem_path = $path . DIRECTORY_SEPARATOR . $filename;                    
        if (is_dir($tem_path)) {                        
        self::addDirectoryToZip($tem_path, $zip);
                    } else {                        
                    $zipObj->addFromString(iconv('gbk', 'utf-8', $tem_path), file_get_contents($tem_path));
                    }
                }
            }
            @closedir($path);            
            $zipObj->close();
        }
    }    /**
     * Create a or more path
     * @param $path
     */
    public static function createPath($path)
    {
        if (is_array($path)) {            
        foreach ($path AS $v) {                
        if (!is_array($v)) {                    
        self::createOnePath($v);
                } else {                    
                die('Must be a one-dimensional array!');
                }
            }
        } else {            
        self::createOnePath($path);
        }
    }    /**
     * Create a path
     * @param $path
     */
    public static function createOnePath($path)
    {
        $path = strpos(basename($path), '.') ? dirname($path) : $path;       
         if (!is_dir($path)) {//nonexistion path
            mkdir($path, 0777, true) ? '' : die('Creation path failed!');
        }
    }
Copy after login

Related recommendations:

A few lines of code to easily implement PHP file packaging and download zip

PHP file locking ensures multi-threaded writing Safety

The above is the detailed content of PHP file compressed package. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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