Adodbzip instance in php

Source: Internet
Author: User
This article mainly introduces the adodbzip class in php and describes in detail the implementation methods and usage skills of the adodbzip class file in the form of examples. the annotations contain detailed function descriptions, which are very useful, for more information, see the examples in this article to describe the adodbzip program code in php. Share it with you for your reference. The details are as follows:

The code is as follows:

<? Php
/**
* AdodbZip 1.1
*
* Sample code:
* Include_once 'adodbzip. php ';
* $ Db = AdodbZip: init (NewADOConnection ('MySQL tutorial t '));
* Echo $ db-> GetOne ('Select NOW ()');
*
* Process description:
* 1. if The Adodb program file in $ extract_dir exists and $ zip_file does not exist, use the Adodb program file in $ extract_dir.
* 2. if The Adodb program file in $ extract_dir exists and $ zip_file exists, compare the modification time. The Adodb program file in $ extract_dir is updated to a newer one.
* 3. if The Adodb program file in $ extract_dir does not exist and $ zip_file exists, the Adodb program file in $ extract_dir is extracted from $ zip_file.
* 4. if The Adodb program file in $ extract_dir does not exist and $ zip_file does not exist, download the Adodb Zip file from $ zip_url and decompress the Adodb program file.
* Other instructions:
* 1. $ extract_dir can be customized. If you decompress the Zip package of Adodb to this directory, you can ignore the $ zip_url and $ zip_file settings, which are the same as the conventional use of Adodb.
* 2. $ zip_file can be customized. If $ zip_file exists, you can ignore $ zip_url, so that you can use $ zip_file for the whole site.
* 3. $ zip_url can be customized. You can modify the Adodb version at any time. in this case, it is best to use the default value for $ zip_file and $ extract_dir. each version does not affect each other.
* 4. $ server, $ user, $ pwd, and $ db can be customized. The default value is the default value of mysql. this option is effective only when the AdodbZip: init method is called.
* 5. $ charset can be customized. The encoding is not changed by default. this option is effective only when the AdodbZip: init method is called.
* 6. AdodbZip: The Adodb initialization value can be added in the init method.
*/

/**
* AdodbZip startup item
*/
// Set parameters
AdodbZip: $ zip_url = 'http: // c.net tutorial examples // [configuration item] Adodb Zip file. it is recommended to download or decompress the file before it is large.
AdodbZip: $ zip_file = sys_get_temp_dir (). preg_replace ('/^. */(adodb .*?. Zip) $/I ', 'adodb/$ 1', AdodbZip: $ zip_url); // [set item] the location of the adodb Zip file cache
AdodbZip: $ entry_dir = 'adodb5 ';
AdodbZip: $ extract_dir = sys_get_temp_dir (). 'adodb/'. AdodbZip: $ entry_dir; // [set item] adodb program file cache location
AdodbZip: $ server = 'localhost'; // [Settings] server
AdodbZip: $ user = 'root'; // [set item] user name
AdodbZip: $ pwd = ''; // [Settings] password
AdodbZip: $ db = 'test'; // [Settings] database tutorial
AdodbZip: $ charset = ''; // [set item] Encoding

// Registration agreement
If (! In_array ('adodbzip ', stream_get_wrappers ())){
Stream_wrapper_register ('adodbzip ', 'adodbzip ');
}
// Define a constant
If (! Defined ('adodb _ dir ')){
Define ('adodb _ dir', 'adodbzip :/');
}
// Contains the program
Require_once (ADODB_DIR. '/adodb. inc. php ');
// $ Db = AdodbZip: init (NewADOConnection ('mysqlt'); // [select item] reference defines $ db
// Return AdodbZip: init (NewADOConnection ('mysqlt'); // [select an item], note that you can only Reference $ db once.

/**
* AdodbZip class definition
*/
Class AdodbZip {

/**
* Adodb variables
*/
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;

/**
* Stream variable
*/
Private $ handle;
Public $ context;

/**
* Adodb function Group
*/

/**
* Init
* @ Param adodb & $ adodb
* @ Return adodb
*/
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;
}

/**
* Stream function Group
*/

/**
* _ Construct
*/
Public function _ construct (){
}

/**
* Stream_cast
* @ Param int $ cast_as
* @ Return resource
*/
Public function stream_cast ($ cast_as ){
Return false;
}

/**
* Stream_close
*/
Public function stream_close (){
Fclose ($ this-> handle );
}

/**
* Stream_eof
* @ Return bool
*/
Public function stream_eof (){
Return feof ($ this-> handle );
}

/**
* Stream_flush
* @ Return bool
*/
Public function stream_flush (){
Return fflush ($ this-> handle );
}

/**
* Stream_lock
* @ Param mode $ options
* @ Return bool
*/
Public function stream_lock ($ options ){
Return flock ($ this-> handle, $ options );
}

/**
* Stream_open
* @ Param string $ path
* @ Param string $ mode
* @ Param int $ options
* @ Param string & $ opend_path
* @ Return bool
*/
Public function stream_open ($ path, $ mode, $ options, & $ opend_path ){
// Verify the file address
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;
// Verify the program file
If (! File_exists ($ tmp_file) | file_exists ($ zip_file) & filectime ($ tmp_file) <filectime ($ zip_file )){
// Download an object
If (! File_exists ($ zip_file )){
// Directory processing
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 ('create a directory '. $ zip_dir );
}
}
// Download an object
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.0 rnHost:". $ 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 ('Download the file'. self: $ zip_url. '.zipSave as '. self: $ zip_file );
}
}
}
// Create a directory
$ 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 ('create a directory '. $ tmp_dir );
}
}
// Open the compressed file
$ Zip = zip_open ($ zip_file );
If (! Is_resource ($ zip )){
Return false;
}
// Find the decompressed file
Do {
$ Entry = zip_read ($ zip );
If (! Is_resource ($ entry )){
Return false;
}
If (zip_entry_name ($ entry) = $ entry_file ){
Break;
}
} While (true );
// Compress the compressed file
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 );
}
// Open the file
$ This-> handle = fopen ($ tmp_file, $ mode );
If (! Is_resource ($ this-> handle )){
Return false;
}
Return true;
}

/**
* Stream_read
* @ Param int $ count
* @ Return string
*/
Public function stream_read ($ count ){
Return fread ($ this-> handle, $ count );
}

/**
* Stream_seek
* @ Param int $ offset
* @ Param int $ whence = SEEK_SET
* @ Return bool
*/
Public function stream_seek ($ offset, $ whence = SEEK_SET ){
Return fseek ($ this-> handle, $ offset, $ whence );
}

/**
* Stream_set_option
* @ Param int $ option
* @ Param int $ arg1
* @ Param int $ arg2
* @ Return bool
*/
Public function stream_set_option ($ option, $ arg1, $ arg2 ){
Return false;
}

/**
* Stream_stat
* @ Return array
*/
Public function stream_stat (){
Return fstat ($ this-> handle );
}

/**
* Stream_tell
* @ Return int
*/
Public function stream_tell (){
Return ftell ($ this-> handle );
}

/**
* Stream_write
* @ Param string $ data
* @ Return int
*/
Public function stream_write ($ data ){
Return fwrite ($ this-> handle, $ data );
}

/**
* Url_stat
* @ Param string $ path
* @ Param int $ flag
* @ Return array
*/
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;
}
}
?>


The example code is as follows:

The code is as follows:

<? Php
Include_once 'adodbzip. php ';
$ Db = AdodbZip: init (NewADOConnection ('mysqlt '));
Echo $ db-> GetOne ('Select NOW ()');
?>


This is also two steps.

1. include the AdodbZip. php file

2. the AdodbZip: init (...) function initializes the adodb connection class.

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.