0

I have a super simple CSV file ( about 500 lines, 14.5k ) which I need to put into an array which Later I will need to check the ID column against other data set .

The CSV is like so :

id,mail,Plan
102,,Plan_Pay
1028,,Plan_Pay
1031,,Plan_Prom
1032,,Plan_Pay
1033,,Plan_Pay
1034,[email protected],Plan_Free
1035,[email protected],Plan_Free
1036,[email protected],Plan_Pay
1079,,Plan_Prom
109,,Plan_Pay
1166,[email protected],Plan_Prom
12,[email protected],Plan_Pay
....
....
(on and on .. about 500 lines)

But anyhow I try to parse it , I get an empty array .

I tried with parsecsv lib

$csvf =  'id2.csv';

echo $csvf; // debug verify that path exists
$csv = new parseCSV();
$csv->auto($csvf);
print_r($csv->data);// results empty

and also with the variant

$csv = new parseCSV('id2.csv');
print_r($csv->data);// results empty

I also tried ( from php.net ):

  if ( !function_exists('fgetcsv') ) echo 'Server too old ... we need to check.';// but it is ok

    function csv_to_array($filename, $delimiter=',')
    {
        if(!file_exists($filename) || !is_readable($filename)){
         $data = array();
        $data[] = 'no file'; // debug -verify there is file..
        // echo 'no file'; // debug -verify there is file..
        // return FALSE;
    }
        $header = NULL;
        $data = array();
        if (($handle = fopen($filename, 'r')) !== FALSE)
        {
            while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
            {
                if(!$header)
                    $header = $row;
                else
                    $data[] = array_combine($header, $row);
            }
            fclose($handle);
        }
        return $data;
    }

    $csv = csv_to_array($csvf);
    echo $csvf; // debug verify correct path
    print_r($csv);// results empty

I tried all path combinations, relative ( "id2.csv" ) , absolute ( "http://path.to/id2.csv" ) etc. but it all results the same .

My question is : can there be something wrong with the file itself ? *.CSV is such a simple format , and I have a very small file ( about 500 lines ). Can it be encoding problem ( it is UTF-8 without bom) . or maybe a server problem ? Or am I just doing it all wrong ?? ( Until now , there were no visible errors anywhere )

5
  • Hate to state the obvious but... do you actually have read access to the file you're trying to open? Commented Jun 8, 2013 at 7:44
  • @Filippos Karapetis , I would Assume I have . it is a localhost server on local machine (xampp) . never had such a problem. also, if the file did not exists, I have an ` if(!file_exists($filename) || !is_readable($filename))` statement - or maybe it is not good enoug ? Commented Jun 8, 2013 at 7:46
  • echo $csvf; does not check if the file/path exists... Commented Jun 8, 2013 at 8:22
  • @Shomz - no, it doesn´t , but if(!file_exists($filename) || !is_readable($filename)) does ( or should ) Commented Jun 8, 2013 at 8:29
  • @ObmerkKronen That's much better. Commented Jun 8, 2013 at 8:31

1 Answer 1

1

From PHP DOC

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.

If you have set the auto_detect_line_endings here is a simpler way to read the csv

$csv = new SplFileObject("id2.csv", "r");
$csv->setFlags(SplFileObject::READ_CSV);
foreach($csv as $row) {
    list($id,$mail,$plan) = $row;
    // You your stuff
}
Sign up to request clarification or add additional context in comments.

9 Comments

I don't believe that this is the problem. If that was the problem with the OP's code, he/she would get an array with a single entry, containing a huge blob of text... However, the OP is getting a totally empty array
@Baba hmmmm.. :-) since you already saved me several times , I tend to believe you :-). But I get this error : Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(id2.csv) .. ( plus, i do not know if relevant , but it is not a Mac file - it was exported from Open Office on WIN7 )
@ObmerkKronen are you saying the file is not read at all ? if so can you please verify if your .csv is small letters or .CSV capital letters ...
@baba - it is small letters . your script is actually running IMHO - because it takes quit a long time to output the error.. it is not immediate ( circa 45 sec ) . so I assume it does SOMETHING.
45 sec ? How big is the file ? do you have xdebug running ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.