dl

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

dl実行時に PHP 拡張モジュールをロードする

説明

dl(string $extension_filename): bool

extension_filename で指定された PHP 拡張モジュールを読み込みます。

その拡張モジュールが既に使用可能かどうかを調べるには、 extension_loaded() を使用します。 これは、組み込みのモジュールと (php.ini か、あるいは dl() を使用して) 動的に読み込むモジュールの両方に対応しています。

警告

この関数は、CLI と、組み込済みの SAPI に対してのみ使えます。 また、コマンドラインから実行している場合にのみ CGI SAPI に対しても使えます。

パラメータ

extension_filename

このパラメータに指定できるのは拡張モジュールの ファイル名だけであり、それはプラットフォームに依存します。 例えば、Unix プラットフォームでは sockets 拡張モジュール (共有モジュールとしてコンパイルされていれば。デフォルトでは有りません!) は sockets.so と呼ばれていますし、一方 Windows プラットフォームでは php_sockets.dll と呼ばれます。

拡張モジュールを読み込むディレクトリは、プラットフォームによって異なります。

Windows - php.ini に明記されていない場合、デフォルトでは 拡張モジュールは、C:\php5\ からロードされます。

Unix - php.ini に明記されていない場合、デフォルトでは 以下に依存します。

  • PHP をビルドする際に --enable-debug を指定しているか否か
  • PHP をビルドする際に ZTS (Zend Thread Safety) サポートを有効にしているか否か
  • 現在の ZEND_MODULE_API_NO(Zend 内部モジュール API 番号。基本的にはメジャーモジュール API の変更が発生した日時。 例:20010901)
上記を考慮して、ディレクトリのデフォルトは <install-dir>/lib/php/extensions/ <debug-or-not>-<zts-or-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 を返します。 拡張モジュールのロード機能が無効だったり、あるいは 無効化されている(enable_dl でオフにされている)場合は、 E_ERROR を発行して実行は停止されます。 指定されたライブラリをロードできず dl() が 失敗した場合、false に加えて E_WARNING メッセージが 発行されます。

例1 dl() の例

<?php
// OS によってロードするファイルを切り替える
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);
}
?>

注意

注意:

dl() は Unix プラットフォーム上では 大文字小文字を区別します。

参考

add a note

User Contributed Notes 2 notes

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

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

?>
To Top