Basic usage
Once your CSV object is instantiated and configured, you can start interacting with the data using a number of methods available to you. For starters, you can iterate over your newly object to extract each CSV row using the foreach
construct.
$reader = Reader::createFromPath('/path/to/my/file.csv', 'r');
foreach ($reader as $index => $row) {
//do something meaningful here with $row !!
//$row is an array where each item represent a CSV data cell
//$index is the CSV row index
}
Outputting the CSV
__toString()
Use the echo
construct on the instantiated object or use the __toString
method to show the CSV full content.
echo $reader;
// or
echo $reader->__toString();
output($filename = null)
If you only wish to make your CSV downloadable by forcing a file download just use the output
method to force the use of the output buffer on the CSV content.
header('Content-Type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="name-for-your-file.csv"');
$reader->output();
die;
The output method can take an optional argument $filename
. When present you
can even remove more headers.
$reader->output("name-for-your-file.csv");
die;
The output methods can only be affected by:
No other method or property have effect on them.