PHP 8.3.4 Released!

dl

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

dlBelirtilen PHP eklentisini çalışma anında yükler

Açıklama

dl(string $eklenti): bool

eklenti bağımsız değişkeni ile belirtilen PHP eklentisini yükler.

Belli bir eklentinin yüklü olup olmadığını sınamak için, hem yerleşik (php.ini ile yüklenen) hem de devingen olarak (dl() ile) yüklenen eklentileri bulan extension_loaded() işlevini kullanın.

Uyarı

Bu işlev komut satırından çağrıldığında yalnızca CGI SAPI, gömülü SAPI'ler ve CLI için kullanılabilir.

Bağımsız Değişkenler

eklenti

Bu bağımsız değişken ile eklentinin platforma özgü kütüphane dosyasının ismi belirtilir. Örneğin sockets eklentisini Windows platformunda yüklemek için php_sockets.dll ismini kullanmak gerekirken Unix platformlarında sockets.so kullanmak gerekir (bir paylaşımlı modül olarak derlenmişse).

Eklentinin yükleneceği dizin platforma göre değişiklik gösterir:

Windows - php.ini içinde açıkça belirtilmemişse, eklenti öntanımlı olarak c:\php5\ dizininden yüklenir.

Unix - php.ini içinde açıkça belirtilmemişse, öntanımlı eklenti dizinin yeri şunlara bağlıdır:

  • PHP'nin --enable-debug seçeneği ile derlenip derlenmediği;
  • PHP'nin ZTS (Zend Thread Safety) desteği ile derlenip derlenmediği;
  • geçerli yerleşik ZEND_MODULE_API_NO sabiti (Zend yerleşik modülü API numarası; genelde API modülünün değişiklik tarihidir. 20010901 gibi).
Bu durumlar hesaba katılarak eklentilerin öntanımlı yeri şöyle saptanır: <kurulum-dizini>/lib/php/extensions/ <debug/no-debug>-<zts/non-zts>-ZEND_MODULE_API_NO. Örnek: /usr/local/php/lib/php/extensions/debug-non-zts-20010901 veya /usr/local/php/lib/php/extensions/no-debug-zts-20010901.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner. Eğer modül yükleme işlevsel değilse (php.ini içinde enable_dl off yapılarak iptal edilmişse) bir E_ERROR iletisi çıktılanıp çalışma durdurulur. Eğer işlev, kütüphane yüklenemediğinden dolayı başarısız olursa bir E_WARNING iletisi çıktılayarak falsedöndürür.

Örnekler

Örnek 1 - dl() örnekleri

<?php
// Platforma göre eklenti yüklemek
if (!extension_loaded('sqlite')) {
if (
strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}

// veya PHP_SHLIB_SUFFIX sabiti de kullanılabilir
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>

Notlar

Bilginize:

dl() Unix platformlarında harf büyüklüğüne duyarlıdır.

Ayrıca Bakınız

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