3.3. PHP Streams

Starting with PHP4.3, PHP's URL wrappers (the things that allow you to use URLs in places where filenames are usually expected) has been extended to become the basic mechanism of file interaction in general. One of the consequences of this is that you can now use URLs in almost any context where a local file path would be appropriate. (Some specialized aspects, such as configuration settings, are exempt from this: you can't have a URL in your include path, for example.) This includes file access functions used by third-party extensions, with PHP now treating local file accesses as requests via file:// URLs.

The other consequence is that it became feasible in PHP5 to expose an interface for constructing custom stream wrappers to the PHP developer in the so-called streams interface.

Programmers who are familiar with Java or C++ will already be familiar with the streams concept; the kioslaves that appear in the K Desktop Environment are another example. In PHP, a stream is an object that behaves like a file—you may be able to read bytes from it, write bytes to it, go to a particular byte position within it, ask how many bytes there are, and so on. Note the use of "may be"—just as some files are read-only and some are write-only, streams too might be read-only or write-only, while some generate their data on the fly so that there is no clearly-defined analog of "file offset" or "file size" or even "end of file." The idea is that users of a stream need have no idea ...

Get Professional LAMP: Linux®, Apache, MySQL®, and PHP5 Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.