简体   繁体   中英

CSV file read fail (PHP )

I am trying to read a CSV file (delimited by commas) but unfortunately, it isn't responding as it ought to. I am not so sure what I am doing wrong here, but I'll paste out the contents of the code and the CSV file both :

$row = 0;
if($handle = fopen("SampleQuizData.csv","r") !== FALSE)
{
    // WORKS UNTIL HERE, SO FILE IS BEING READ
    while(!feof(handle))
    {
        $line = fgetcsv($handle, 1024, ",") ;
        echo $line[2];  // DOES NOT WORK
    }    
}

Here is the CSV file: (the emails and names have been changed here to protect the identities of the users)

parijat,something,parijatYkalia@hotmail.com
matthew,durp, mdurpdurp@gmail.com
steve,vai,stevevai@gmail.com
rajni,kanth,rajnikanth@superman.com

it lacks a '$' to the handle variable

while(!feof($handle)){

and not :

while(!feof(handle)){

Give this a try:

<?php

$row = 0;

if (($handle = fopen("SampleQuizData.csv", "r")) !== FALSE)
{
    while(!feof($handle))
    {
        $line = fgetcsv($handle, 1024, ",") ;
        echo "$line[2]";
    }    
}

?>

It's worth a mention but when I was working on CSV exports a few weeks ago, I had weird line ending inconsistencies. So I put this at the top of my php file and it worked splendid.

<?php
    ini_set("auto_detect_line_endings", true);
?>

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