Import export CSV file using PHP

Today we talk about a very important feature of the data management system is Import and Export CSV file in PHP script from front-end. Using Import action you can save bulk data on the database using your website. And on the Export user can download the complete database table data for their external use.

Download complete code from below link

Import and Export CSV file in PHP

Step by step process of Import and Export CSV file in PHP

On this we use .csv (comma-separated values) format for import and export operation. In this task, the import and export script, the following functionality will be implemented.

  • Fetch users from the database and listed on the web page.
  • Import and Export CSV file in PHP and MySQL.
  • Export or download database table data on CSV format using PHP and MySQL.

Create a database table

To store the user’s data we have to create a table on MySQL database. The below MySQL code create the table named members. Copy the whole code and paste it on PHPMYADMIN SQL tab and click on GO.

CSV File Format

When you start the import operation on the web page your CSV file looks like this,

You can see the database table data which is imported through the CSV, after importing the CSV in the database using PHP and MySQL.

Complete source code of Import and Export CSV file in PHP

Database Configuration (db.php)

On the database configuration file, we create connection and specify the database username, host, database name and database password.

On this above database configuration code, mysqli_connect is function and the first localhost is the host of the server, second is database username, third is database password and the last parameter is database Name.

CSV file Import and Export (index.php)

On this, there is an HTML tabular format where imported data shows and from there you can do below-listed operations.

  • Existing data shows on the HTML table.
  • An import button used to import CSV file whit these available database fields.
    • On the import operation, after clicking the import button, the form is submitted to importData.php file.
    • We use formToggle() function to show or hide the import form.
  • On the page, if the data is already imported than success message is showing.
  • On above the table there is an export button also which is used for download the imported data.

Import CSV file to database (importData.php)

The importData.php file has all the operation check of importing the CSV file using PHP and MySQL.

  • Check the CSV file is valid or not.
  • Check the upload status of CSV file using PHP is_uploaded_file() function.
  • Using PHP fopen() function, we open the CSV file.
  • Fetch data from the CSV file using PHP fgetcsv() function.

 Export table data as CSV (exportData.php)

The exportData.php file has all the operation checks of the export process using PHP and MySQL.

  • Create and open a file with writing-only mode using the PHP fopen() function.
  • Fetch the data from the database.
  • Set header columns, CSV format and opened file using PHP fputcsv() function.
  • Force browser to download data as CSV format in a file.

This script helps you to integrate the Import and Export CSV file in PHP and MySQL in the database. If you have any query please comment below.

Check Out: jQuery autocomplete ajax with Text and Image

[sociallocker]Click And Download Complete Source[/sociallocker]

Happy Coding..!

Was this article helpful?

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

3 thoughts on “Import and Export CSV file in PHP”

Leave a Reply

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