Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: dl - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

extension_loaded" width="11" height="7"/> <assert
Last updated: Thu, 31 May 2007

view this page in

dl

(PHP 4, PHP 5)

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

説明

int dl ( string $library )

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

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

パラメータ

library

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

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

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

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 でオフにされているか または php.iniセーフモード が有効になっている)場合は、 E_ERROR を発行して実行は停止されます。 指定されたライブラリをロードできず dl() が 失敗した場合、FALSE に加えて E_WARNING メッセージが 発行されます。

例 1783. dl() の例

<?php
// OS によってロードするファイルを切り替える
if (!extension_loaded('sqlite')) {
    if (
strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
       
dl('php_sqlite.dll');
    } else {
       
dl('sqlite.so');
    }
}

// PHP 4.3.0 では PHP_SHLIB_SUFFIX 定数を利用することも可能
if (!extension_loaded('sqlite')) {
   
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
   
dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}
?>

注意

注意: dl() はマルチスレッド Web サーバ上では サポートされません。 そのような環境の場合には php.ini 上で extensions 命令を使用するようにしてください。しかし、CGICLI には 影響しません!

注意: PHP 5 では、CLI 以外の あらゆる SAPI において dl() 関数が非推奨となっています。 代わりに、拡張モジュール 読み込み用のディレクティブを利用してください。

注意: PHP 6 以降では、CLI, CGI および組み込み版 (embed) を除くすべての SAPI でこの関数は無効となっています。

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

注意: この関数は、safe-mode では無効となります。

参考

拡張モジュール読み込み用のディレクティブ
extension_loaded()



extension_loaded" width="11" height="7"/> <assert
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
dl
buildsmart at daleenterprise dot com
30-Dec-2006 06:33
I recently came across this under PHP 4.4.4, it seems that the dl(); function generates an error/warning about registering the function if a test is done on an extension that is pre-loaded in the php.ini file (extension=gd.so).
<?php
$gd_is_shared           
= "shared-library";

if (
function_exists('ImageCreateFromPNG') && !@dl('gd.so')) {
   
$gd_is_shared = "embedded";
}

print
$gd_is_shared;
?>

The only purpose of this test is to determine if it is an embedded extension or a loaded extension.

I don't see this error occur under PHP 5.1.6 or PHP 5.2.0.

The test platform is Mac OS X 10.3.9 and Mac OS X 10.4.8
prozente
23-Nov-2006 08:57
a note for mag_2000 at front dot ru
the line
$currentExtPath = ini_get('extension_dir');

would be better to be

$currentExtPath = realpath(ini_get('extension_dir'));

I came across some hosts that had ./  set at the extension_dir
docey
31-Dec-2005 02:37
just some note to loading modules, they do not have to
be executable.

some examples below check for this but if an module is
not executable is does not mean you cant use it. it just
needs to be readable NOT executable.

although some modules might need this perhaps for some
reason i cannot think of, so here an example,

// fails to load mysql although it could be loaded.
if(is_executable("mysql.so")){
 dl("mysql.so");
}

// loads mysql
if(is_readable("mysql.so")){
 dl("mysql.so");
}

watch out with this, as you can see mysql.so would not be
loaded and the script would fail. because its checked for
executable permissions although these are not needed.
mag_2000 at front dot ru
07-Dec-2005 04:13
<?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()

?>
james at gogo dot co dot nz
19-Jul-2005 08:30
WARNING: enable_dl/dl()
*********************

There is an exploit circulating currently which takes advantage of dl() to inject code into Apache which causes all requests to all virtual hosts to be redirected to a page of the attackers choice.

All operators of shared web hosting servers with Apache and PHP should disable dl() by setting enable_dl to off otherwise your servers are vulnerable to this exploit.

This exploit is generally known as flame.so (the object that is loaded into Apache) and flame.php (the php script that loads it).

Google gives more information:
http://www.google.co.nz/search?q=flame.so+flame.php
nutbar at innocent dot com
19-Mar-2004 06:22
For those who are tearing their hair out due to the extension_dir being set to "./", here's a somewhat graceful fix that will be portable:

dl(preg_replace('/\/([^\/]+)/', '../', dirname(__FILE__)) . __FILE__ . '/../some/path/dll.so');

You could alternatively make that preg_replace part into a separate function and call it something like dirname_rel() or whatever.  It basically replaces all the path components with ".." and spits out a relative path, so that when mixed with the "./" part of the extension_dir setting, it puts you at the root folder "/" so that you know where you are :)
endofyourself at yahoo dot com
18-Oct-2003 08:12
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

---------------
/*
    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()

-------------------------
tychay at php dot net
06-Jul-2003 12:15
Alan isn't 100% correct (though he's close). The exception is Mac OS X. This operating system makes a distinction between dynamically loadable shared libraries and and loadable modules of code (bundles). The former has an extension .dylib and the latter has an extension .so. The former is in Mac-O and the latter is in ELF.

Thus PHP's extensions are built as .so whereas the symbol PHP_SHLIB_SUFFIX is bound (currently) to .dylib. I don't think this is the correct behavior, but nonetheless, it is the behavior as of PHP-5.0.0b2-dev. Right now, the config binds to SHLIB_SUFFIX_NAME (which is correctly bound to .dylib on Mac OS X). I imagine this is related to why there is so much trouble getting dl() to work on Mac OS X. (For instance, I have no trouble phpizing in a new shared library, but when compiling in stuff as shared... much evilness!)

BTW, to get dl() to work in Mac OS X you need to install the dlcompat library (via Fink, DarwinPorts, or Gentoo ports). Remember in the case of Fink, you better make sure your environment variables are adjusted to point to where dlcompat (and your other fink libraries) are.

terry

extension_loaded" width="11" height="7"/> <assert
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites