compression - Turn directory into Zip with PHP -
How do I change a directory in PHP with a ZIP file?
Thank you.
to "".
There is a simple function that can compress any file or directory repeatedly, it requires only zip extension to load.
function zip ($ source, $ destination) {if (extension_loaded ('zip') === true) {if (file_exists ($ source) === true) {$ zip = New zipper (); If ($ zip-> open ($ Destination, Write :: Create) === true) {$ source = realpath ($ source); If (is_dir ($ source) === true) {$ files = new recursive iterator iterator (new source), recursiveitter iterator :: SELF_First); Foreign Currency ($ file $ files) {$ file = realpath ($ file); If (is_dir ($ file) === true) {$ zip- & gt; AddEmptyDir (str_replace ($ source. '/', '', $ File. / /)); } And if (is_file ($ file) === true) {$ zip- & gt; AddFromString (str_replace ($ source. '/', '', $ File), file_get_contents ($ file)); }}} And if (is_file ($ source) === true) {$ zip- & gt; AddFromString (BaseName ($ source), file_get_contents ($ source)); }} Back to $ zip- & gt; Stop it (); } } return false; }
Call it like this:
zip ('/ folder / in / press /', '. / Compressed.zip');
Edit - This folder will not contain folder structure (see my comment):
Function zip ($ Source, $ destination) {if (extension_loaded ('zip') === true) {if (file_exists ($ source) === true) {$ zip = new ziparchive (); If ($ zip-> open ($ destination, zinechii :: created) === true) {if (is_dir ($ source) === true) {$ files = new recursiveIteratorIterator (new recurring directoryitter (source $ ), RecursiveTitator :: SELF_FIRST); Foreign Currency ($ files as $ $) {if (is_file ($ file) is === true) {$ zip- & gt; AddFromString (original name ($ file), file_gate_content ($ file)); }}} And if (is_file ($ source) === true) {$ zip- & gt; AddFromString (BaseName ($ source), file_get_contents ($ source)); }} Back to $ zip- & gt; Stop it (); } } return false; }
Comments
Post a Comment