简体   繁体   中英

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?

Problems:

  1. You try to open a remote file with w+ (write) access. Use r for read.
  2. You check f (undefined constant) . Use . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM