PHP Conference Japan 2024

dl

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

dl於執行時期載入 PHP 擴充

說明

dl(字串 $extension_filename): 布林值

載入由參數 extension_filename 指定的 PHP 擴充。

使用 extension_loaded() 來測試指定的擴充是否已可用。這適用於內建擴充和動態載入的擴充(透過 php.inidl())。

警告

此函數僅適用於 CLI 和嵌入式 SAPI,以及從命令列執行的 CGI SAPI

參數

extension_filename

此參數是要載入的擴充功能檔名,也取決於您的平台。例如,sockets 擴充功能(如果編譯為共享模組,而不是預設值!)在 Unix 平台上稱為 sockets.so,而在 Windows 平台上稱為 php_sockets.dll

載入擴充功能的目錄取決於您的平台

Windows - 如果未在 php.ini 中明確設定,則預設會從 C:\php5\ 載入擴充功能。

Unix - 如果未在 php.ini 中明確設定,則預設的擴充功能目錄取決於

  • PHP 是否使用 --enable-debug 建置
  • PHP 是否使用 ZTS(Zend 執行緒安全)支援建置
  • 目前的內部 ZEND_MODULE_API_NO(Zend 內部模組 API 編號,基本上是發生主要模組 API 變更的日期,例如 20010901
考慮到上述因素,目錄預設為 <安裝目錄>/lib/php/extensions/ <debug-或-not>-<zts-或-not>-ZEND_MODULE_API_NO,例如 /usr/local/php/lib/php/extensions/debug-non-zts-20010901/usr/local/php/lib/php/extensions/no-debug-zts-20010901

傳回值

成功時傳回 true,失敗時傳回 false。如果載入模組的功能不可用或已被停用(透過在 php.ini 中設定 enable_dl 為 off),則會發出 E_ERROR 並停止執行。如果 dl() 因為無法載入指定的程式庫而失敗,除了 false 之外,還會發出 E_WARNING 訊息。

範例

範例 #1 dl() 範例

<?php
// 根據作業系統載入擴充功能的範例
if (!extension_loaded('sqlite')) {
if (
strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
dl('php_sqlite.dll');
} else {
dl('sqlite.so');
}
}

// 或使用 PHP_SHLIB_SUFFIX 常數
if (!extension_loaded('sqlite')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>

備註

備註:

在 Unix 平台上,dl() 區分大小寫。

參見

新增備註

使用者貢獻的備註 4 則備註

shaunspiller at spammenot-gmail dot com
15 年前
dl 很難用,因為檔名格式與作業系統相關,而且如果擴展已經載入,它會發出抱怨。這個包裝函式解決了這個問題

<?php

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

?>

範例

<?php

// 確保我們支援 SSL 和 MySQL
load_lib('openssl');
load_lib('mysql');

// 少數擴展的檔名與其擴展名稱不同,例如 image (gd) 函式庫,因此我們這樣指定它們:
load_lib('gd', 'gd2');

?>
mag_2000 at front dot ru
18 年前
<?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()

?>
anrdaemon at freemail dot ru
8 年前
與 eval() 一樣,使用 dl() 的唯一正確方法是不使用它。
測試您打算使用的函式是否可用。
如果沒有,請向使用者提出抱怨或實作解決方法。
更不用說 dl() 在多執行緒環境中的問題。
endofyourself at yahoo dot com
21 年前
如果您需要從「目前」的本地目錄載入擴展,因為您沒有權限將擴展放置在伺服器的 PHP 擴展目錄中,那麼我寫的這個函式可能對您有用

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