ExpertRefresh

Streams / Read csv  

1) Reads a row from a previously opened CSV file








fgetcsv() - Gets line from file pointer and parse for CSV fields <?php // Reads a row from a previously opened CSV file // into an enumerative array $fd = fopen("example.csv", "r"); while($row = fgetcsv($fd)) { print_r($row); // Array ( [0] => aa [1] => bb [2] => cc ) // Array ( [0] => dd [1] => ee [2] => ff ) } fputcsv() - Writes the elements of an array in CSV format to an open file handle <?php $fd = fopen("example.csv", "w"); $arr = array(1,2,3); fputcsv($fd, $arr); // Output: 1,2,3 $arr = array('a', 'b', 'c'); fputcsv($fd, $arr, ";"); // Output: a;b;c $arr = array('"a"', '"b"', '"c"'); fputcsv($fd, $arr, ";"); // Output: """a""";"""b""";"""c""" $arr = array('"a"', '"b"', '"c"'); fputcsv($fd, $arr, ";", "'"); // default delimiter: " // Output: "a";"b";"c" fpassthru Reads all data from the current position in an open file, until EOF, and writes the result to the output buffer. <?php /* -- example.txt aaa bbb ccc */ $file = fopen("example.txt","r"); fgets($file); // Read first line fpassthru($file); // Send rest of the file to the output buffer // Outputs: aaa rn bbb fclose($file);


References