Search code examples
phpfilefputcsv

php fputcsv is trims the string value while writing to file


I don't know this question is asked or not. I didn't get his in suggestion.

I'm trying to write some data from database to an csv file. I'm using fputcsv function.

I'm using following code :

$fh = @fopen($fileName, 'w+');
        $writeText['sku'] = $somevalue
        $writeText['attribute_set'] = $somevalue
        $writeText['type'] = $somevalue
        $writeText['default_catalogue_link']= $somevalue
        $writeText['status'] = $somevalue
@fputcsv($fh, $writeText);
        @flush();
@fclose($fh);

Some of the values starts with zero like $writeText['sku'] is 0987. After writing to the file. I'm getting 987 in the file.

how to prevent this? I need with zero values.

Any help would be usefull. Thanks in advance.


Solution

  • Are you using Excel to open the file? Because Excel interprets it as a number. Try open and see with a text editor.