How to create a zip file using PHP

前端 未结 6 1553
难免孤独
难免孤独 2020-11-29 02:40

I am trying to save files from one folder to another. zip folder placed in different directory. And I have written the fol

6条回答
  •  情书的邮戳
    2020-11-29 02:57

    $zip = new ZipArchive();
    
    $DelFilePath="first.zip";
    
    if(file_exists($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath)) {
    
            unlink ($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath); 
    
    }
    if ($zip->open($_SERVER['DOCUMENT_ROOT']."/TEST/".$DelFilePath, ZIPARCHIVE::CREATE) != TRUE) {
            die ("Could not open archive");
    }
        $zip->addFile("file_path","file_name");
    
    // close and save archive
    
    $zip->close(); 
    

    Here TEST is your project folder name.

    You can define path as you want.

提交回复
热议问题