PHP Upload and Read CSV File

Purpose of writing this post to show an example of how to upload and read csv file using PHP code. CSV is a comma-separated value which commonly used to store data like Excel.

Read CSV File in PHP

PHP has an inbuilt function fgetcsv() which parse a line from an open CSV file.

Syntax: fgetcsv (file,length,separator,enclosure)

Here file and length are required parameter and the other two are optional. The length specifies the maximum length of line. separator parameter use as file separator like (,).

Upload CSV file in PHP:

To upload CSV file use input html tag as type “file” value. use below HTML code to achieve this:

PHP Read CSV File Code:

Let assume we can upload and read CSV file on the same PHP file page. After submitting the upload CSV file get the form post and use fopen and fgetscv function to read CSV file. Use below PHP parse CSV file code:

In this read csv file code, each cell value will get in $data array variable. You can parse this array to get separated value. That’s it, very easy to implement this functionality.

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

2 thoughts on “PHP Upload and Read CSV File”

Leave a Comment

Your email address will not be published. Required fields are marked *