Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP compress files into zip

Hello there okay so this is the deal i have a folder where my mp3 files are stored for single download, and now i want a feature where you can download all the mp3s at once in a zipped folder. Is it possible to zip all the files from that folder using php ( or anything really but if not PHP then please explain thoroughly) or would i have to just re-upload all the mp3s again in zipped format?

Thanks you so much.

like image 554
DonJuma Avatar asked Mar 08 '26 22:03

DonJuma


1 Answers

Just to demonstrate use of the undocumented addGlob() method of the zipArchive class:

$zipFile = dirname(__FILE__)."/myMP3s.zip";
$zipArchive = new ZipArchive();

if (!$zipArchive->open($zipFile, ZIPARCHIVE::OVERWRITE))
    die("Failed to create archive\n");

$zipArchive->addGlob(dirname(__FILE__)."/*.mp3");
if (!$zipArchive->status == ZIPARCHIVE::ER_OK)
    echo "Failed to write local files to zip\n";

$zipArchive->close();
like image 136
Mark Baker Avatar answered Mar 11 '26 12:03

Mark Baker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!