download this file: class.dl.php view text/plain: class.dl.php file encoding: UTF-8 [goback]
<?php
##
## loading dynamic library(shared object file, extension modules)
## san2(at)linuxchannel.net
## 2002.10.19
##
## $mod = array() or string(file path)
## example :
##   new ext_load('./4.2.3/mbstring.so');
##   or
##   new ext_load(array('4.2.3/mbstring.so','4.2.3/korean.so'));
##   or
##   new ext_load('/any/path/to/korean.so')
##

class ext_load
{
  function 
ext_load($mod$debug=0)
  {
    if(!
ini_get('enable_dl') || !$mod) {
        if(
$debug) echo 'can not use the dl() function';
        return 
0;
      }

    
$dir['e'] = ini_get('extension_dir');
    
$dir['u'] = dirname($_SERVER['PATH_TRANSLATED']).'/'// script file directory
    
$dir['ut'] = preg_replace(';[^/]+;','..',$dir['u']);

    if(
preg_match(';^/;',$dir['e'])) {
        
$dir['e'] = preg_replace(';[^/]+;','..',$dir['e']);
    }
    else 
$dir['e'] = '';

    if(
is_array($mod)) {
        foreach(
$mod AS $v) {
            
$file $this->get_file($dir,$v);
            
$this->to_load($file,$debug);
        }
      } else {
        
$file $this->get_file($dir,$mod);
        
$this->to_load($file,$debug);
    }
  }

  function 
get_file($dir$v)
  {
    if(
preg_match(';^/;',$v)) {
        if(!
$dir['e']) return $dir['ut'] . $v;
        else return 
$dir['e'] . $v;
    } else {
        if(!
$dir['e']) return $v;
        else return 
$dir['e'] . $dir['u'] . $v;
    }
  }

  function 
to_load($file$debug=0)
  {
    if(
is_file($file)) {
        
$ext str_replace('.so','',basename($file));
        if(!
extension_loaded($ext)) {
            @
dl($file);
            if(!
extension_loaded($ext)) {
                echo 
"to loading ${ext}.so : fail<BR>\n";
            } else if(
$debug) {
                echo 
"to loading ${ext}.so : loaded<BR>\n";
            }
        }
        else if(
$debug) echo "alredy, loaded ${ext}.so<BR>\n";
    }
    else if(
$debug) echo "not found $file<BR>\n";
  }
// end of class


/***
## example
##
$useto = array('calendar.so','dba.so','dio.so','ftp.so','gd.so','gettext.so',
    'mbstring.so','sockets.so','sysvsem.so','sysvshm.so','korean.so');
foreach($useto AS $v) $mod[] = $_SERVER['DOCUMENT_ROOT'] . '/ext/' . PHP_VERSION .'/'.$v;

new ext_load($mod,1); // debuging mode
phpinfo();
***/

?>