PHP

HOW TO ADD FILES & FOLDERS WITH PHP ZIP ARCHIVE

OPEN ZIP FILE $zip = new ZipArchive();  $zip->open("ARCHIVE.ZIP", ZipArchive::CREATE);

ADD FILE FOLDER TO ZIP (A)

01

ADD A FILE TO ZIP $zip->addFile("SOURCE.FILE", "ZIP.FILE");

ADD EMPTY FOLDER IN ZIP $zip->addEmptyDir("folder");

ADD FILE FOLDER TO ZIP (B)

02

FROM STRING TO FILE $zip->addFromString("string.txt", "This is a string!");

DONE - CLOSE ZIP $zip->close();

OPEN ZIP FILE $zip = new ZipArchive();  $zip->open("ARCHIVE.ZIP", ZipArchive::CREATE);

ADD FOLDER USING GLOB

03

ADD ALL IMAGES IN FOLDER $zip->addGlob(   "FOLDER/*.{jpg,png,gif}", GLOB_BRACE, [   "add_path"=>"/", "remove_all_path"=>true ]);

DONE - CLOSE ZIP $zip->close();

OPEN ZIP FILE $zip = new ZipArchive();  $zip->open("ARCHIVE.ZIP", ZipArchive::CREATE);

ADD FOLDER USING PATTERN

04

ADD ALL IMAGES IN FOLDER $zip->addPattern(   "/\.(?:jpg|png)$/", "/FOLDER/", [   "add_path"=>"/", "remove_all_path"=>true ]);

DONE - CLOSE ZIP $zip->close()