Raj
Raj

Reputation: 1437

fgetcsv with newline issue in php

I have a csv file on windows system which I am getting from a mac and the data looks like

ID526526526526

So you can see there is no new line no delimiter but still it says csv file.However I have the following code which is

$csvFile = 'MonC1.csv';
$allRowsAsArray = array();
if (!$fp=fopen($csvFile,"r")) echo "The file could not be opened.<br/>";  
while (( $data = fgetcsv ( $fp , 0 , ',')) !== FALSE ) 
{
    $allRowsAsArray[] = $data;
}

echo '<pre>';
print_r($allRowsAsArray );
echo '</pre>';

Now when I print the data in browser it looks like

    Array
    (
        [0] => Array
            (
                [0] => ID
                526
                526
                526
            )
   );

Now I am stuck as I can't create another properly delimited csv file with new line using fputcsv.

Any help is highly welcomed. Thanks in advance.

Upvotes: 2

Views: 5660

Answers (1)

slapyo
slapyo

Reputation: 2991

It's having a problem detecting the line endings. Try turning on auto_detect_line_endings before you run fopen.

ini_set('auto_detect_line_endings', TRUE);
$csvFile = 'MonC1.csv';
$allRowsAsArray = array();
if (!$fp=fopen($csvFile,"r")) echo "The file could not be opened.<br/>";  
while (( $data = fgetcsv ( $fp , 0 , ',')) !== FALSE ) 
{
    $allRowsAsArray[] = $data;
}

echo '<pre>';
print_r($allRowsAsArray );
echo '</pre>';

Upvotes: 7

Related Questions