Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable the zip extension for PHP

I am trying to enable the .zip extension in PHP, but the function below returns false.

if (!extension_loaded('zip')) {
    return false;
}

How do I enable the .zip extension with out using php.ini?

Is it possible to enable using ini_set()?

like image 955
Lucky13 Avatar asked Mar 12 '13 09:03

Lucky13


1 Answers

Provided that you actually have the ZIP extension available on the server, you could use dl() to dynamically load it (<5.3).

if (!extension_loaded('zip')) {
    // Attempt to load the zip
    $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
    dl($prefix . 'zip.' . PHP_SHLIB_SUFFIX);

    if (!extension_loaded('zip')) {
        // Couldn't load the ZIP module dynamically, either
        return false;
    }
}

If you're using a version above 5.3.0, you won't be able to use dl unless it's running on the command line or embedded into a web server.

That leaves your only option to be modification of the php.ini if you can't recompile with the module built-in to PHP. You can't do this using ini_set, as that will only be applied at runtime whilst all of the required modules will already have been loaded by the PHP executable at startup.

like image 64
Rudi Visser Avatar answered Sep 22 '22 15:09

Rudi Visser