Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php压缩同一目录中的所有文件和文件夹_Php_Zip - Fatal编程技术网

php压缩同一目录中的所有文件和文件夹

php压缩同一目录中的所有文件和文件夹,php,zip,Php,Zip,如何使用php zip压缩同一目录中的所有文件和文件夹 所以基本上我有一个文件和文件夹的列表,我想用php压缩所有东西。抱歉,我不熟悉php zip,php net上的文档帮助不大您可以使用此功能创建文件的zip文件夹: public function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is f

如何使用php zip压缩同一目录中的所有文件和文件夹


所以基本上我有一个文件和文件夹的列表,我想用php压缩所有东西。抱歉,我不熟悉php zip,php net上的文档帮助不大

您可以使用此功能创建文件的zip文件夹:

public function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        //close the zip -- done!
        $zip->close();

        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}

此处,$files将包含数组中的所有文件。$destination将是您在服务器上压缩文件的位置。

Ronak我知道如何压缩特定的文件,我想压缩所有文件和文件夹,使其成为主文件夹的备份