Using the PHP zip function without exec
The PHP function exec is disabled on our servers, see PHP security: disabled functions. This means you can't call the server's zip and unzip programs. You don't need to, though: PHP has its own ZipArchive class. You can use it to create zip files and extract them.
Creating a zip file
Save this code as testzip.php, for example in public_html, and open the file in your browser:
<?php
$zip = new ZipArchive();
$bestand = __DIR__ . '/test.zip';
if ($zip->open($bestand, ZipArchive::CREATE) !== true) {
exit("Het lukt niet om $bestand te openen. Heeft de map schrijfrechten?");
}
$zip->addFile(__DIR__ . '/testzip.php', 'testzip.php');
echo 'Aantal bestanden in de zip: ' . $zip->numFiles;
$zip->close();
Extracting a zip file
<?php
$zip = new ZipArchive();
if ($zip->open(__DIR__ . '/test.zip') === true) {
$zip->extractTo(__DIR__ . '/uitgepakt');
$zip->close();
echo 'Uitgepakt';
} else {
echo 'Het zip-bestand kon niet worden geopend';
}
Not working? Then check in Select PHP version whether the zip extension is enabled. Delete the test script when you're done.
Just want to extract a zip file you uploaded yourself? You can also do that without code in File Manager, see I want to upload and extract a zip file.
Detailed explanation with screenshots: Enabling and disabling PHP extensions in the DirectAdmin manual.
