8.2 Wrapper Overview

 <  Day Day Up  >  

As long as there's a wrapper that supports it, you can use streams to read from and write to any location or protocol using the same set of file manipulation functions from the previous section.

For example, here are a few streams that access nonlocal files:

  • http://www.example.com/index.html

  • https ://secure.example.com/secret.html

  • ftp:// user :password@ftp.example.com/www/www.example.com/index.html

The first stream makes an HTTP request to www.example.com and retrieves the file from the Internet. The second makes a secure HTTP connection using SSL and accesses secret.html from secure.example.com . The final wrapper transfers a file using FTP, passing along a username and password in the process.

However, while streams do abstract away most implementation specifics, they can't alter the inherent nature of the protocol itself. For instance, although it's quite easy to read files using HTTP, most web servers are not set up to allow you to save files using HTTP. Likewise, when you use FTP, you need to provide a username and password or the FTP server won't be able to authenticate you as a legitimate user.

8.2.1 Bundled Wrappers

Before getting into the specifics of each individual wrapper, here's a brief overview of all the wrappers bundled with PHP 5, as well as the additional wrappers you can enable with certain extensions.

PHP 5 automatically provides you with four wrappers:


file

Talks to the local filesystem


http

Requests a file from a web server


ftp

Reads and writes files from an FTP server


php

Handles reading from standard in and writing to standard out and error.

Compiling in certain extensions enables four additional wrappers:


compress.bzip2

Automatically (de)compresses bzipped files on the fly; requires BZip2 support


compress.zlib

Automatically (de)compresses gzipped files on the fly; requires zlib support


ftps

Securely reads and writes files from an FTP server; requires OpenSSL


https

Securely reads files from an HTTP server; requires OpenSSL

During the configuration process, enable bzip2 compression support with --with-bz2 , zlib compression with --with-zlib , and OpenSSL with --with-openssl .

The stream_get_wrappers( ) function returns an array of available wrappers. Use this to discover which filters are available on your machine:

 print_r(stream_get_wrappers( )); 

which prints:

  Array   (   [0] => php   [1] => file   [2] => http   [3] => ftp   [4] => compress.zlib   )  

This copy of PHP has the basic set of wrappers and the zlib wrapper, but not the extra SSL or bzip2 wrappers.

 <  Day Day Up  >  


Upgrading to PHP 5
Upgrading to PHP 5
ISBN: 0596006365
EAN: 2147483647
Year: 2004
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net