Day 24 – Streams

Zend PHP Certification

I only have one week before my certification exam!  I’m shivering in my boots (if I had boots)!  Well, the important part is not to worry and keep studying.  Here is today’s topic (and the last chapter of the book):

The streams layer is a series of classes/methods in PHP that manage the reading and writing to various data sources (file, network, compression, etc.).  Here are the basic commands you need to know regarding any stream:

  • $stream = fopen($URI, $mode): This opens a connection to a stream specified by the URI.  The mode can be:
    • r: reading only, starting in front
    • r+: reading/writing, starting in front
    • w: writing only, starting in front, truncates contents
    • w+: writing/reading, starting in front, truncates contents
    • a: writing only, starts at end
    • a+: reading/writing, starts at end
    • x: creates new file for writing
    • x+: creates new file for writing/reading

    Note: w, w+, a, a+ create the file if it does not already exists. x and x+ will throw a warning if the file already exists. You can also append a modifier after the mode: b for binary and t for Windows line breaks.

  • $line = fgets($stream, [$length]): This reads in data until either a newline is found, $length bytes were read, or EOF.
  • $char = fgetc($stream): This gets one character from the stream
  • $data = fread($stream, $length): This reads in data (binary-safe) until either $length bytes were read or EOF.
  • $array = fscanf($stream, $format): Reads in data from a stream based off a format provided (can also provide additional parameters to write to).
  • $bytes = fwrite($stream, $string, [$length]): Writes $string to the stream and returns number of bytes sent.
  • fclose($stream): Closes the stream.

PHP has a large portion of built in classes including:

Standard Input/Output

When running PHP as a script and not a website, you can interact with the console screen like any other C/C++ application.  The most basic forms are to use php://stdin (const STDIN), php://stdout (const STDOUT), and php://stderr (const STDERR).  Now most streams need to be opened and closed, but with PHP and these standard inputs you do not need to do so (due to some buggy issues).  Here is an example of reading and writing:

fwrite(STDOUT, “What is your name? “);
  1. $name = trim(fgets(STDIN))
  2. fprintf(STDOUT, "Hello, %s", $name);

There are also some fun streams for filtering incoming data and writing to temp/memory.  But it’s not really important right now.

File Access

This is really the most widely used form of stream access in PHP.  There are loads of functions designed to perform various actions on the file in question.  Here are some of them:

  • $bytes = filesize($filename): Returns the number of bytes in a file
  • is_file($filename): Checks if a file is there or not
  • is_writable($filename): Checks if a file is writable
  • is_readable($filename): Checks if a file is readable
  • ftruncate($stream, $size): Truncates a file stream to $size
  • fseek($stream, $offset): Moves the file pointer to $offset bytes (many options available)
  • rewind($stream): Goes to the front of the file pointer (same as fseek($stream, 0))
  • $position = ftell($stream): Returns the current position in the file
  • $array = fgetcsv($stream): Reads in a line from the file, attempting to parse for CSV format
  • fputcsv($stream, $array): Writes a line to the file and places the values from $array in CSV format

An example of this will come tomorrow!

No Comments

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>