(PHP 4, PHP 5, PHP 7, PHP 8)
dl — Loads a PHP extension at runtime
DescriptionUse extension_loaded() to test whether a given extension is already available or not. This works on both built-in extensions and dynamically loaded ones (either through php.ini or dl()).
WarningThis function is only available for the CLI and embed SAPIs, and the CGI SAPI when run from the command line.
Parametersextension_filename
This parameter is only the filename of the extension to load which also depends on your platform. For example, the sockets extension (if compiled as a shared module, not the default!) would be called sockets.so on Unix platforms whereas it is called php_sockets.dll on the Windows platform.
The directory where the extension is loaded from depends on your platform:
Windows - If not explicitly set in the php.ini, the extension is loaded from C:\php5\ by default.
Unix - If not explicitly set in the php.ini, the default extension directory depends on
--enable-debug
or not ZEND_MODULE_API_NO
(Zend internal module API number, which is basically the date on which a major module API change happened, e.g. 20010901
) <install-dir>/lib/php/extensions/ <debug-or-not>-<zts-or-not>-ZEND_MODULE_API_NO
, e.g. /usr/local/php/lib/php/extensions/debug-non-zts-20010901 or /usr/local/php/lib/php/extensions/no-debug-zts-20010901.
Returns true
on success or false
on failure. If the functionality of loading modules is not available or has been disabled (by setting enable_dl off in php.ini) an E_ERROR
is emitted and execution is stopped. If dl() fails because the specified library couldn't be loaded, in addition to false
an E_WARNING
message is emitted.
Example #1 dl() examples
<?php
// Example loading an extension based on OS
if (!extension_loaded('sqlite')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}// Or using PHP_SHLIB_SUFFIX constant
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>
Notes
shaunspiller at spammenot-gmail dot com ¶Note:
dl() is case sensitive on Unix platforms.
16 years ago
dl is awkward because the filename format is OS-dependent and because it can complain if the extension is already loaded. This wrapper function fixes that:
<?phpfunction load_lib($n, $f = null) {
return extension_loaded($n) or dl(((PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '') . ($f ? $f : $n) . '.' . PHP_SHLIB_SUFFIX);
}?>
Examples:
<?phpload_lib('openssl');
load_lib('mysql');load_lib('gd', 'gd2');?>
mag_2000 at front dot ru ¶
19 years ago
<?phpfunction dl_local( $extensionFile ) {
if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
die( "dh_local(): Loading extensions is not permitted.\n" );
}if( !file_exists( $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' does not exist.\n" );
}if( !is_executable( $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' is not executable.\n" );
}$currentDir = getcwd() . "/";
$currentExtPath = ini_get( "extension_dir" );
$subDirs = preg_match_all( "/\//" , $currentExtPath , $matches );
unset( $matches );if( !(bool)$subDirs ) {
die( "dl_local(): Could not determine a valid extension path [extension_dir].\n" );
}$extPathLastChar = strlen( $currentExtPath ) - 1;
if(
$extPathLastChar == strrpos( $currentExtPath , "/" ) ) {
$subDirs--;
}$backDirStr = "";
for( $i = 1; $i <= $subDirs; $i++ ) {
$backDirStr .= "..";
if( $i != $subDirs ) {
$backDirStr .= "/";
}
}$finalExtPath = $backDirStr . $currentDir . $extensionFile;if( !dl( $finalExtPath ) ) {
die();
}$loadedExtensions = get_loaded_extensions();
$thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];return $thisExtName;
}
?>
anrdaemon at freemail dot ru ¶
8 years ago
Like with eval(), the only correct way to use dl() is to not use it.
Test if a function(s) you intend to use are available.
If not, complain to the user or implement a workaround.
Not to mention dl() issues in a multithreading environment.
21 years ago
If you need to load an extension from the CURRENT local directory because you do not have privelages to place the extension in your servers PHP extensions directory, this function i wrote may be of use to you
<?php
function dl_local( $extensionFile ) {
if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
die( "dh_local(): Loading extensions is not permitted.\n" );
} if( !file_exists( $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' does not exist.\n" );
} if( !is_executable( $extensionFile ) ) {
die( "dl_local(): File '$extensionFile' is not executable.\n" );
} $currentDir = getcwd() . "/";
$currentExtPath = ini_get( "extension_dir" );
$subDirs = preg_match_all( "/\//" , $currentExtPath , $matches );
unset( $matches ); if( !(bool)$subDirs ) {
die( "dl_local(): Could not determine a valid extension path [extension_dir].\n" );
} $extPathLastChar = strlen( $currentExtPath ) - 1;
if(
$extPathLastChar == strrpos( $currentExtPath , "/" ) ) {
$subDirs--;
} $backDirStr = "";
for( $i = 1; $i <= $subDirs; $i++ ) {
$backDirStr .= "..";
if( $i != $subDirs ) {
$backDirStr .= "/";
}
} $finalExtPath = $backDirStr . $currentDir . $extensionFile; if( !dl( $finalExtPath ) ) {
die();
} $loadedExtensions = get_loaded_extensions();
$thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ]; return $thisExtName;
}
?>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3