How to import a csv file using php

3 minute read

CSV (comma separated values) files of data are a common medium for importing data into web applications.  For example you may wish to import product data from a Microsoft Excel or Google spreadsheet into your Magento database.  

There is no way to do this currently with the Zend Framework, however we have written a handy PHP component to make the task easy. PHP has some useful native functions for handling CSV files.

In particular this component uses the fgetcsv function which parses a csv file line and returns an array of it’s values.   This function is much better than using php’s explode function as it automatically handles field values which contain commas such as ‘Gravitywell Web Development, Bristol’.   Please note that locale settings may affect how it works.

All you need to do is pass the file path of your CSV file to the function and it will parse the file and return a multi dimensional array of the data for you to insert into your database.

Happy coding!