25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

242 ” Streams and Network Programming<br />

$file = fopen(’counter.txt’, ’r+’);<br />

fseek($file, 10, SEEK_SET);<br />

You should keep in mind that value of the second parameter is added to the position<br />

you specify as a starting point. Therefore, when your starting position is SEEK_END,<br />

this number should always be zero or less, while, when you use SEEK_SET, it should<br />

always be zero or more. When you specify SEEK_CURR as a starting point, the value<br />

can be either positive (move forward) or negative (move backwards)—in this case, a<br />

value of zero, while perfectly legal, makes no sense.<br />

i<br />

To find the current position of the pointer, you should use ftell().<br />

The last two functions that we are going to examine here are fgetcsv() and<br />

fputcsv(), which vastly simplify the task of accessing CSV files. As you can imagine,<br />

the former reads a row from a previously-opened CSV file into an enumerative<br />

array, while the latter writes the elements of an array in CSV format to an open file<br />

handle.<br />

Both of these functions require a file handle as their first argument, and accept an<br />

optional delimiter and enclosure character as their last two arguments:<br />

$f = fopen(’file.csv’);<br />

while ($row = fgetcsv($f)) {<br />

// handle values<br />

}<br />

$values = array("Davey Shafik", "http://zceguide.com", "Win Prizes!");<br />

fputcsv($f, $values);<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

If you don’t specify a delimiter and an enclosure character, both fgetcsv() and<br />

fputcsv() use a comma and quotation marks respectively.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!