Muhammad Umar
Muhammad Umar

Reputation: 11782

fgetCsv Returning null values for csv file in php

I have a csv file placed at path

#http://thevowapp.com/brandstore/values.csv

$f = fopen("http://thevowapp.com/brandstore/values.csv", "w+");  
if(!f)
{
    echo "Error";
}
$line = fgetcsv($f);
echo json_encode($line);

I am trying to parse it, however the fgetCsv keeps on returning null. What could be the error?

Upvotes: 0

Views: 1994

Answers (1)

scrowler
scrowler

Reputation: 24406

Problems:

  1. You try to open a remote file with w+ (write) access. Use r for read.
  2. You check f (undefined constant). Use$f`.
  3. You don't loop fgetcsv, so you won't get more than the header line.

Try:

$f = fopen('http://thevowapp.com/brandstore/values.csv', 'r');
if(!$f) {
    echo 'Error';
    exit;
}

$out = array();
while ($line = fgetcsv($f)) {
    $out[] = $line;
}

echo json_encode($out, JSON_PRETTY_PRINT);

Remove the pretty print option when you're happy.

Upvotes: 1

Related Questions