Stream wrappers

If you've been writing PHP for very long, you have most likely needed to work with local or remote files at some point. The following PHP code is a common way to read a file into a variable that you can do something with:

$contents = ";
$handle = fopen("/var/www/htdocs/images/xyzzy.jpg", "rb");
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);

This is pretty straightforward. You get a handle to a local file using fopen() and read 8 KB chunks of the file using fread() until feof() indicates that you've reached the end of the file, at which point you fclose() the handle. The contents of the file are now in the variable $contents. In addition to local files, you can also access remote files through fopen() ...

Get Drupal 7 Module Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.