1$file = fopen('file.csv', 'r');
2while (($line = fgetcsv($file)) !== FALSE) {
3 print_r($line);
4}
5fclose($file);
1
2<?php
3$row = 1;
4if (($handle = fopen("test.csv", "r")) !== FALSE) {
5 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
6 $num = count($data);
7 echo "<p> $num fields in line $row: <br /></p>\n";
8 $row++;
9 for ($c=0; $c < $num; $c++) {
10 echo $data[$c] . "<br />\n";
11 }
12 }
13 fclose($handle);
14}
15?>
16
17
1 $csvFile = file('../somefile.csv');
2 $data = [];
3 foreach ($csvFile as $line) {
4 $data[] = str_getcsv($line);
5 }
1// output headers so that the file is downloaded rather than displayed
2header('Content-Type: text/csv; charset=utf-8');
3header('Content-Disposition: attachment; filename=data.csv');
4
5// create a file pointer connected to the output stream
6$output = fopen('php://output', 'w');
7
8// output the column headings
9fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
10
11// fetch the data
12mysql_connect('localhost', 'username', 'password');
13mysql_select_db('database');
14$rows = mysql_query('SELECT field1,field2,field3 FROM table');
15
16// loop over the rows, outputting them
17while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
18