PHP 8.3.4 Released!

dl

(PHP 4, PHP 5, PHP 7, PHP 8)

dlLädt eine PHP-Erweiterung zur Laufzeit

Beschreibung

dl(string $extension_filename): bool

Lädt die im Parameter extension_filename angegebene PHP-Erweiterung.

Verwenden Sie extension_loaded() um zu testen, ob die gewünschte Erweiterung bereits verfügbar ist oder nicht. Dies funktioniert sowohl bei eingebauten, als auch bei dynamisch geladenen Erweiterungen (entweder durch die php.ini, oder mittels dl()).

Warnung

Diese Funktion ist nur für die CLI- und die eingebetteten SAPIs sowie für die CGI-SAPI verfügbar, sofern sie über die Befehlszeile ausgeführt wird.

Parameter-Liste

extension_filename

Dieser Parameter ist nur der Dateiname der zu ladenden Erweiterung, welcher von der Plattform abhängig ist, z. B. würde die Erweiterung sockets (wenn als shared module kompiliert, nicht standardmäßig!) auf Unix-Plattformen sockets.so heißen, und auf Windows-Plattformen php_sockets.dll.

Das Verzeichnis, von wo aus die Erweiterung geladen wird, hängt von der Plattform ab:

Windows - Wenn nicht explizit in der php.ini angegeben, wird die Erweiterung standardmäßig von c:\php5\ geladen.

Unix - Wenn nicht explizit in der php.ini angegeben, hängt das standardmäßige Verzeichnis ab von:

  • ob PHP mit --enable-debug erstellt wurde oder nicht
  • ob PHP mit ZTS- (Zend Thread Safety) Unterstützung erstellt wurde oder nicht
  • der aktuellen internen ZEND_MODULE_API_NO (Zend-interne Modul-API-Nummer, welche im Grunde das Datum der letzten größeren Modul-API-Änderung darstellt, z. B. 20010901).
Das obige in Betracht gezogen, ist das standardmäßige Verzeichnis <install-dir>/lib/php/extensions/<debug-or-not>-<zts-or-not>-ZEND_MODULE_API_NO, z. B. /usr/local/php/lib/php/extensions/debug-non-zts-20010901 oder /usr/local/php/lib/php/extensions/no-debug-zts-20010901.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben. Ist die Funktionalität des Ladens von Modulen nicht verfügbar, oder wurde sie deaktiviert (durch Deaktivieren von enable_dl in der php.ini), wird ein E_ERROR ausgegeben, und die Ausführung gestoppt. Scheitert dl(), weil die angegebene Erweiterung nicht geladen werden konnte, wird zusätzlich zu false eine E_WARNING-Meldung ausgegeben.

Beispiele

Beispiel #1 dl()-Beispiele

<?php
// Beispiel für das Laden einer Erweiterung je nach Betriebssystem
if (!extension_loaded('sqlite')) {
if (
strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}

// Oder unter Verwendung der Konstante PHP_SHLIB_SUFFIX
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>

Anmerkungen

Hinweis:

dl() unterscheidet auf Unix-Plattformen zwischen Groß- und Kleinschreibung.

Siehe auch

add a note

User Contributed Notes 4 notes

up
13
shaunspiller at spammenot-gmail dot com
15 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:

<?php

function load_lib($n, $f = null) {
return
extension_loaded($n) or dl(((PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '') . ($f ? $f : $n) . '.' . PHP_SHLIB_SUFFIX);
}

?>

Examples:

<?php

// ensure we have SSL and MySQL support
load_lib('openssl');
load_lib('mysql');

// a rare few extensions have a different filename to their extension name, such as the image (gd) library, so we specify them like this:
load_lib('gd', 'gd2');

?>
up
-2
mag_2000 at front dot ru
18 years ago
<?php

function dl_local( $extensionFile ) {
//make sure that we are ABLE to load libraries
if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
die(
"dh_local(): Loading extensions is not permitted.\n" );
}

//check to make sure the file exists
if( !file_exists( $extensionFile ) ) {
die(
"dl_local(): File '$extensionFile' does not exist.\n" );
}

//check the file permissions
if( !is_executable( $extensionFile ) ) {
die(
"dl_local(): File '$extensionFile' is not executable.\n" );
}

//we figure out the path
$currentDir = getcwd() . "/";
$currentExtPath = ini_get( "extension_dir" );
$subDirs = preg_match_all( "/\//" , $currentExtPath , $matches );
unset(
$matches );

//lets make sure we extracted a valid extension path
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 .= "/";
}
}

//construct the final path to load
$finalExtPath = $backDirStr . $currentDir . $extensionFile;

//now we execute dl() to actually load the module
if( !dl( $finalExtPath ) ) {
die();
}

//if the module was loaded correctly, we must bow grab the module name
$loadedExtensions = get_loaded_extensions();
$thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];

//lastly, we return the extension name
return $thisExtName;

}
//end dl_local()

?>
up
-4
anrdaemon at freemail dot ru
7 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.
up
-4
endofyourself at yahoo dot com
20 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()
Reference: http://us2.php.net/manual/en/function.dl.php
Author: Brendon Crawford <endofyourself |AT| yahoo>
Usage: dl_local( "mylib.so" );
Returns: Extension Name (NOT the extension filename however)
NOTE:
This function can be used when you need to load a PHP extension (module,shared object,etc..),
but you do not have sufficient privelages to place the extension in the proper directory where it can be loaded. This function
will load the extension from the CURRENT WORKING DIRECTORY only.
If you need to see which functions are available within a certain extension,
use "get_extension_funcs()". Documentation for this can be found at
"http://us2.php.net/manual/en/function.get-extension-funcs.php".
*/

function dl_local( $extensionFile ) {
//make sure that we are ABLE to load libraries
if( !(bool)ini_get( "enable_dl" ) || (bool)ini_get( "safe_mode" ) ) {
die(
"dh_local(): Loading extensions is not permitted.\n" );
}

//check to make sure the file exists
if( !file_exists( $extensionFile ) ) {
die(
"dl_local(): File '$extensionFile' does not exist.\n" );
}

//check the file permissions
if( !is_executable( $extensionFile ) ) {
die(
"dl_local(): File '$extensionFile' is not executable.\n" );
}

//we figure out the path
$currentDir = getcwd() . "/";
$currentExtPath = ini_get( "extension_dir" );
$subDirs = preg_match_all( "/\//" , $currentExtPath , $matches );
unset(
$matches );

//lets make sure we extracted a valid extension path
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 .= "/";
}
}

//construct the final path to load
$finalExtPath = $backDirStr . $currentDir . $extensionFile;

//now we execute dl() to actually load the module
if( !dl( $finalExtPath ) ) {
die();
}

//if the module was loaded correctly, we must bow grab the module name
$loadedExtensions = get_loaded_extensions();
$thisExtName = $loadedExtensions[ sizeof( $loadedExtensions ) - 1 ];

//lastly, we return the extension name
return $thisExtName;

}
//end dl_local()

?>
To Top