Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabrysiak/163cb9d56a9e31d715e5 to your computer and use it in GitHub Desktop.
Save gabrysiak/163cb9d56a9e31d715e5 to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function fgetcsv_PHP()
{
/*
* See if we can open a file named fgetcsv.csv in
* read mode, if we can then assign pointer to this
* file to a variable named $handle
* 'r' - Open for reading only; place the file
* pointer at the beginning of the file.
*/
if (($handle = fopen("markers.csv", "r")) !== FALSE)
{
/*
* fgetcsv( resource $handle int $length string $delimiter )
*
* resource $handle
* A valid file pointer to a file successfully opened by fopen(),
* popen(), or fsockopen().
*
* int $length
* Must be greater than the longest line (in characters) to be
* found in the CSV file (allowing for trailing line-end characters).
* It became optional in PHP 5. Omitting this parameter (or setting
* it to 0 in PHP 5.0.4 and later) the maximum line length is not
* limited, which is slightly slower.
*
* string $delimiter
* Set the field delimiter (one character only).
*
* RETURN VALUES
*
* Returns an indexed array containing the fields read.
*
* Note: A blank line in a CSV file will be returned as an array
* comprising a single null field, and will not be treated
* as an error.
*
* Note: If PHP is not properly recognizing the line endings when
* reading files either on or created by a Macintosh computer,
* enabling the auto_detect_line_endings run-time configuration
* option may help resolve the problem.
*
* fgetcsv() returns NULL if an invalid handle is supplied or FALSE
* on other errors, and when the end of file has been reached.
*/
$length = 5000;
$delimiter = ",";
/*
* Print the opening table tag to begin buiding HTML table
* and the first row of the table; with column names
*/
$csvData = array();
/*
* Loop through the array of values returned by fgetcsv until there are
* no more lines (indicated by FALSE)
*/
// while ( ( $data = fgetcsv( $handle, $length, $delimiter ) ) !== FALSE )
// {
// // Count number of array elements in $data
// $num = count($data);
// for ($c=0; $c < $num; $c++)
// {
// // if ($c == 1){
// // echo "<td>".$data[1]."<br>".$data[2]."<br>".$data[3]."<br>".$data[4]."<br>".$data[5]."<br>".$data[6]."<br>".$data[7]."</td>\n";
// // } else {
// // echo "<td>".$data[$c]."</td>\n";
// // }
// if($c == 1){
// $csvData[$c][] =
// $data[1]."<br>".$data[2]." ".$data[3]."<br>".$data[4].", ".$data[5].", ".$data[6]."<br>".$data[7];
// } else {
// $csvData[$c][] = $data[$c];
// }
// }
// }
while (($line = fgetcsv($handle)) !==FALSE )
{
if ($line[1]){
$line[1] = $line[1]."<br>".$line[2]." ".$line[3]."<br>".$line[4]." ".$line[5]."<br>".$line[6];
}
$a[] = $line;
}
// Close the file pointed to by $handle
fclose($handle);
}
return $array = str_replace('"', '', $a);
}
function outputCSV($data) {
$outputBuffer = fopen("php://output", 'w');
foreach($data as $val) {
fputcsv($outputBuffer, $val);
}
fclose($outputBuffer);
}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=example.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV(fgetcsv_PHP());
// print_r(fgetcsv_PHP());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment