简体   繁体   中英

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() ?

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.

Use this,

if (!extension_loaded('zip'))
{ 
    $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
    dl($prefix . 'zip.' . PHP_SHLIB_SUFFIX);
    if (!extension_loaded('zip')) 
    {
       return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM