php adodbzip 类
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- AdodbZip::$zip_url = 'http://c.net教程works-kr-1.dl.sourceforge.net/project/adodb/adodb-php5-only/adodb-509-for-php5/adodb509.zip'; //[设置项]Adodb的Zip文件下载地址,文件比较大建议先下载或者解压
- AdodbZip::$zip_file = sys_get_temp_dir () . preg_replace ( '/^.*/(adodb.*?.zip)$/i', 'adodb/$1', AdodbZip::$zip_url );
- AdodbZip::$entry_dir = 'adodb5';
- AdodbZip::$extract_dir = sys_get_temp_dir () . 'adodb/' . AdodbZip::$entry_dir;
- AdodbZip::$server = 'localhost';
- AdodbZip::$user = 'root';
- AdodbZip::$pwd = '';
- AdodbZip::$db = 'test';
- AdodbZip::$charset = '';
-
-
-
- if (! in_array ( 'AdodbZip', stream_get_wrappers () )) {
- stream_wrapper_register ( 'AdodbZip', 'AdodbZip' );
- }
-
- if (! defined ( 'ADODB_DIR' )) {
- define ( 'ADODB_DIR', 'AdodbZip:/' );
- }
-
- require_once (ADODB_DIR . '/adodb.inc.php');
-
-
-
-
-
-
-
- class AdodbZip {
-
-
-
-
- public static $zip_url;
- public static $zip_file;
- public static $entry_dir;
- public static $extract_dir;
- public static $server;
- public static $user;
- public static $pwd;
- public static $db;
- public static $charset;
-
-
-
-
- private $handle;
- public $context;
-
-
-
-
-
-
-
-
-
-
- public static function init(&$adodb) {
- $adodb->Connect ( self::$server, self::$user, self::$pwd, self::$db );
- if(self::$charset!=''){
- $adodb->Execute('SET NAMES '.self::$charset.';');
- }
- return $adodb;
- }
-
-
-
-
-
-
-
-
- public function __construct() {
- }
-
-
-
-
-
-
- public function stream_cast($cast_as) {
- return false;
- }
-
-
-
-
- public function stream_close() {
- fclose ( $this->handle );
- }
-
-
-
-
-
- public function stream_eof() {
- return feof ( $this->handle );
- }
-
-
-
-
-
- public function stream_flush() {
- return fflush ( $this->handle );
- }
-
-
-
-
-
-
- public function stream_lock($options) {
- return flock ( $this->handle, $options );
- }
-
-
-
-
-
-
-
-
-
- public function stream_open($path, $mode, $options, &$opend_path) {
-
- if (! preg_match ( '/^.*?://(.*)$/', $path, $matches )) {
- return false;
- }
- $tmp_file = self::$extract_dir . DIRECTORY_SEPARATOR . $matches [1];
- $entry_file = self::$entry_dir . '/' . str_replace ( '\', '/', $matches [1] );
- $zip_file = self::$zip_file;
-
- if (! file_exists ( $tmp_file ) || file_exists ( $zip_file ) && filectime ( $tmp_file ) < filectime ( $zip_file )) {
-
- if (! file_exists ( $zip_file )) {
-
- if (! is_dir ( dirname ( self::$zip_file ) )) {
- if (mkdir ( dirname ( self::$zip_file ), 0777, true ) === false) {
- header ( 'Content-type: text/html;charset=utf-8' );
- die ( '请创建目录 ' . $zip_dir );
- }
- }
-
- if (! file_exists ( self::$zip_file )) {
- $break = true;
- do {
- $url_arr = parse_url ( self::$zip_url );
- $fp = fsockopen ( $url_arr ['host'], isset ( $url_arr ['port'] ) ? ( int ) $url_arr ['port'] : 80, $errno, $errstr, 10 );
- if ($fp === false) {
- break;
- }
- $out = "GET " . $url_arr ['path'] . " HTTP/1.0rnHost: " . $url_arr ['host'] . " rnConnection: closernrn";
- fputs ( $fp, $out );
- if (feof ( $fp )) {
- break;
- }
- $buffer = fgets ( $fp, 1024 );
- if (! preg_match ( '/^HTTP/1.d 200 /i', $buffer )) {
- break;
- }
- $content_length = false;
- $content_start = false;
- while ( ! feof ( $fp ) ) {
- $buffer = fgets ( $fp, 1024 );
- if ($buffer === "rn") {
- $content_start = true;
- break;
- }
- if (preg_match ( '/^Content-Length:s*(d+)/i', $buffer, $matches )) {
- $content_length = ( int ) $matches [1];
- }
- }
- if ($content_length === false || $content_start === false) {
- break;
- }
- $content = stream_get_contents ( $fp );
- if ($content === false) {
- break;
- }
- $result = file_put_contents ( self::$zip_file, $content );
- unset ( $content );
- if ($result === false) {
- break;
- }
- fclose ( $fp );
- } while ( $break = false );
- if ($break) {
- header ( 'Content-type: text/html;charset=utf-8' );
- die ( '请下载文件 <a href="' . self::$zip_url . '">' . self::$zip_url . '.zip</a > 保存为 ' . self::$zip_file );
- }
- }
- }
-
- $tmp_dir = dirname ( $tmp_file );
- if (! is_dir ( $tmp_dir )) {
- if (mkdir ( $tmp_dir, 0777, true ) === false) {
- header ( 'Content-type: text/html;charset=utf-8' );
- die ( '请创建目录 ' . $tmp_dir );
- }
- }
-
- $zip = zip_open ( $zip_file );
- if (! is_resource ( $zip )) {
- return false;
- }
-
- do {
- $entry = zip_read ( $zip );
- if (! is_resource ( $entry )) {
- return false;
- }
- if (zip_entry_name ( $entry ) == $entry_file) {
- break;
- }
- } while ( true );
-
- zip_entry_open ( $zip, $entry );
- file_put_contents ( $tmp_file, zip_entry_read ( $entry, zip_entry_filesize ( $entry ) ) );
- zip_entry_close ( $entry );
- zip_close ( $zip );
- }
-
- $this->handle = fopen ( $tmp_file, $mode );
- if (! is_resource ( $this->handle )) {
- return false;
- }
- return true;
- }
-
-
-
-
-
-
- public function stream_read($count) {
- return fread ( $this->handle, $count );
- }
-
-
-
-
-
-
-
- public function stream_seek($offset, $whence = SEEK_SET) {
- return fseek ( $this->handle, $offset, $whence );
- }
-
-
-
-
-
-
-
-
- public function stream_set_option($option, $arg1, $arg2) {
- return false;
- }
-
-
-
-
-
- public function stream_stat() {
- return fstat ( $this->handle );
- }
-
-
-
-
-
- public function stream_tell() {
- return ftell ( $this->handle );
- }
-
-
-
-
-
-
- public function stream_write($data) {
- return fwrite ( $this->handle, $data );
- }
-
-
-
-
-
-
-
- public function url_stat($path, $flag) {
- if (! preg_match ( '/^.*?://(.*)$/', $path, $matches )) {
- return false;
- }
- $tmp_file = self::$extract_dir . DIRECTORY_SEPARATOR . $matches [1];
- if (file_exists ( $tmp_file )) {
- if ($flag & STREAM_URL_STAT_LINK) {
- return lstat ( $tmp_file );
- } else {
- return stat ( $tmp_file );
- }
- }
- if ($flag & STREAM_URL_STAT_QUIET) {
- $arr = array ('dev' => 0, 'ino' => 0, 'mode' => 0, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0 );
- return array_merge ( array_values ( $arr ), $arr );
- }
- return false;
- }
- }
- ?>
使用实例代码如下:
- <?php
- include_once 'AdodbZip.php';
- $db = AdodbZip::init(NewADOConnection('mysqlt'));
- echo $db->GetOne('SELECT NOW()');
- ?>
也是两步.
1. 包含AdodbZip.php文件
2. AdodbZip::init(...)函数对adodb连接类进行初始化
|