zend_php_5_certification_study_guide

Page 247

228 ” Streams and Network Programming

$file = fopen(’counter.txt’, ’r+’); fseek($file, 10, SEEK_SET);

You should keep in mind that value of the second parameter is added to the position you specify as a starting point. Therefore, when your starting position is SEEK_END, this number should always be zero or less, while, when you use SEEK_SET, it should always be zero or more. When you specify SEEK_CURRENT as a starting point, the value can be either positive (move forward) or negative (move backwards)—in this case, a value of zero, while perfectly legal, makes no sense.

i

To find the current position of the pointer, you should use ftell().

The last two functions that we are going to examine here are fgetcsv() and which vastly simplify the task of accessing CSV files. As you can imagine, the former reads a row from a previously-opened CSV file into an enumerative array, while the latter writes the elements of an array in CSV format to an open file handle. Both of these functions require a file handle as their first argument, and accept an optional delimiter and enclosure character as their last two arguments: fputcsv(),

$f = fopen(’file.csv’); while ($row = fgetcsv($f)) { // handle values } $values = array("Davey Shafik", "http://zceguide.com", "Win Prizes!"); fputcsv($f, $values);

If you don’t specify a delimiter and an enclosure character, both fgetcsv() and fputcsv() use a comma and quotation marks respectively.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.