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 )
echo $csvf;does not check if the file/path exists...if(!file_exists($filename) || !is_readable($filename))does ( or should )