{"id":525,"date":"2019-09-07T20:15:36","date_gmt":"2019-09-07T20:15:36","guid":{"rendered":"http:\/\/www.programmingfields.com\/?p=525"},"modified":"2026-05-02T06:32:34","modified_gmt":"2026-05-02T06:32:34","slug":"php-import-csv-file-with-preview","status":"publish","type":"post","link":"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/","title":{"rendered":"PHP Import CSV File Data into MySQL Database with Preview"},"content":{"rendered":"\n<p>We can import the CSV files in PHP and store it into the database. The CSV (Comma-Separated Value) data can be imported easily using<strong> PHP Import CSV<\/strong> procedure. When we have a bunch of data to insert it into the database, then, in that case, inserting records one by one is very time-consuming. So to make it more easy and convenient, we can create a data-sheet in CSV format and then the CSV data can be imported easily to the database. So, here I am going to show you how to <strong>upload CSV file in PHP<\/strong>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#PHP_Import_CSV_File\" >PHP Import CSV File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#PHP_Import_CSV_Project\" >PHP Import CSV Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#Create_Database_and_Connection\" >Create Database and Connection<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#Upload_CSV_File_in_PHP\" >Upload CSV File in PHP<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#Create_ImportController_File\" >Create ImportController File<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/programmingfields.com\/php-import-csv-file-with-preview\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PHP_Import_CSV_File\"><\/span>PHP Import CSV File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It is not possible to insert each record through the form when you have to insert the large data. Inserting a single-single record each time is a huge time taking process. So to fix these issues we can insert the data one time by importing it. <\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/check-if-email-available-in-php-using-ajax\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Check If Email Available in PHP MySQL Using AJAX (opens in a new tab)\">Check If Email Available in PHP MySQL Using AJAX<\/a><\/p>\n\n\n\n<p>While importing the CSV file, every single line will be iterated one by one through the loop. The function will read the data line by line and on every iteration, we&#8217;ll be storing the record into the database. So, let&#8217;s create a new project in PHP.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PHP_Import_CSV_Project\"><\/span>PHP Import CSV Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>I will be using a <a rel=\"noreferrer noopener\" aria-label=\"PHP file upload (opens in a new tab)\" href=\"https:\/\/www.programmingfields.com\/php-file-upload-using-jquery-and-ajax\/\" target=\"_blank\">PHP file upload<\/a> concept, in which we&#8217;ll upload a datasheet that will be in CSV format. So, in the very first step, I will create a PHP file that will contain a form with an input type file and a button. <\/p>\n\n\n\n<p>Create the <code> index.php <\/code> file and paste the code.<\/p>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;Import CSV in PHP &lt;\/title&gt;\n\t&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\t&lt;div class=\"container\"&gt;\n\t\t&lt;form method=\"post\" enctype=\"multipart\/form-data\"&gt;\n\t\t\t&lt;div class=\"row mt-5\"&gt;\n\t\t\t\t&lt;div class=\"col-md-6 m-auto border\"&gt;\n\t\t\t\t\t&lt;label&gt; Import Data &lt;\/label&gt;\n\t\t\t\t\t\t&lt;div class=\"form-group\"&gt;\t\t\t\t\t\n\t\t\t\t\t\t\t&lt;input type=\"file\" name=\"file\" class=\"form-control\"&gt;\n\t\t\t\t\t\t&lt;\/div&gt;\n\t\t\t\t\t\t&lt;div class=\"form-group\"&gt;\n\t\t\t\t\t\t\t&lt;button type=\"submit\" name=\"import\" class=\"btn btn-success\"&gt; Import Data &lt;\/button&gt;\n\t\t\t\t\t\t&lt;\/div&gt;\t\t\t\n\t\t\t\t&lt;\/div&gt;\t\t\n\t\t\t&lt;\/div&gt;\n\t\t&lt;\/form&gt;\n\t&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>When you will run the above code, the result will look like this.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"249\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/1.-Import-CSV-in-PHP.png?resize=683%2C249&#038;ssl=1\" alt=\"upload csv\" class=\"wp-image-548\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/1.-Import-CSV-in-PHP.png?w=683&amp;ssl=1 683w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/1.-Import-CSV-in-PHP.png?resize=300%2C109&amp;ssl=1 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><figcaption class=\"wp-element-caption\">Import CSV File<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/jquery-file-upload-in-php\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Drag and Drop Multiple File Upload in PHP Using Dropzone js (opens in a new tab)\">Drag and Drop Multiple File Upload in PHP Using Dropzone js<\/a><\/p>\n\n\n\n<p>Create an excel sheet with some data. Here, I have created a sheet for some products. I will be importing these data.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"610\" height=\"304\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-data.jpg?resize=610%2C304&#038;ssl=1\" alt=\"CSV File\" class=\"wp-image-555\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-data.jpg?w=610&amp;ssl=1 610w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-data.jpg?resize=300%2C150&amp;ssl=1 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><figcaption class=\"wp-element-caption\"><strong>CSV File Data<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>Now, according to the above data, I will create a table in the database. So let&#8217;s connect our application to the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_Database_and_Connection\"><\/span>Create Database and Connection<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Here, my database is <strong>csv_importer <\/strong>and inside this database, I have created a table named <strong>products<\/strong>.<\/li>\n\n\n\n<li>Below, is the database structure. You simply create a database and paste the code there. <\/li>\n<\/ul>\n\n\n\n<pre title=\"Create Database\" class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">--\n-- Database: `csv_importer`\n--\n\n-- --------------------------------------------------------\n\n--\n-- Table structure for table `products`\n--\n\nCREATE TABLE `products` (\n  `product_id` int(11) NOT NULL,\n  `product_name` varchar(100) NOT NULL,\n  `sku` varchar(50) NOT NULL,\n  `brand` varchar(50) NOT NULL,\n  `quantity` int(11) NOT NULL,\n  `price_per_unit` varchar(50) NOT NULL,\n  `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n  `updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;\n\n--\n-- Indexes for dumped tables\n--\n\n--\n-- Indexes for table `products`\n--\nALTER TABLE `products`\n  ADD PRIMARY KEY (`product_id`);\n\n--\n-- AUTO_INCREMENT for dumped tables\n--\n\n--\n-- AUTO_INCREMENT for table `products`\n--\nALTER TABLE `products`\n  MODIFY `product_id` int(11) NOT NULL AUTO_INCREMENT;\nCOMMIT;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For database connection, create a separate folder and inside that folder create a file. <\/li>\n\n\n\n<li>In my case, I&#8217;ve created a folder named <strong>config <\/strong>and inside this folder, I have created a <strong>db-config.php<\/strong> file for creating a connection.<\/li>\n\n\n\n<li>So, the folder structure will look like this.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"201\" height=\"88\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/config-file.jpg?resize=201%2C88&#038;ssl=1\" alt=\"upload csv file in php\" class=\"wp-image-559\"\/><figcaption class=\"wp-element-caption\"><strong>Folder Structure of Config File<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/php-file-upload-using-jquery-and-ajax\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"PHP File Upload Using jQuery and Ajax without Form Refresh (opens in a new tab)\">PHP File Upload Using jQuery and Ajax without Form Refresh<\/a><\/p>\n\n\n\n<p>Now, paste this code inside the <strong>db-config.php<\/strong> file.<\/p>\n\n\n\n<pre title=\"db-config.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\n    class DBController {\n        private $hostname       =       \"localhost\";\n        private $username       =       \"root\";\n        private $password       =       \"root\";\n        private $db             =       \"csv_importer\";\n\n        \/\/ Creating connection\n        public function connect() {\n            $conn               =       new mysqli($this-&gt;hostname, $this-&gt;username, $this-&gt;password, $this-&gt;db)or die(\"Database connection error.\" . $conn-&gt;connect_error);\n            return $conn;           \n        }\n\n        \/\/ Closing connection\n        public function close($conn) {\n            $conn-&gt;close();\n        }\n    }\n?&gt;<\/code><\/pre>\n\n\n\n<p>Make sure, you have replaced your database credentials in the above code. Now, moving to the function to upload CSV file in PHP.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/dropdown-selection-filter-in-php-using-jquery-ajax\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Dependent Dropdown Selection Filter in PHP Using jQuery and AJAX (opens in a new tab)\">Dependent Dropdown Selection Filter in PHP Using jQuery and AJAX<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Upload_CSV_File_in_PHP\"><\/span>Upload CSV File in PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>For uploading the CSV file, I will use the OOPs approach. So, I will create a Controller file in which, I will create the function for uploading the CSV file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open index.php file and paste the below code at the top of the HTML.<\/li>\n<\/ul>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php \n\t\n\t\/\/ including db config file\n\tinclude_once '.\/config\/db-config.php';\n\n\t\/\/ including import controller file\n\tinclude_once '.\/controllers\/import-controller.php';\n\n\t\/\/ creating object of DBController class\n\t$db              =    \tnew DBController();\n\n\t\/\/ calling connect() function using object\n    $conn            =    \t$db-&gt;connect();\n\n    \/\/ creating object of import controller and passing connection object as a parameter\n\t$importCtrl      =    \tnew ImportController($conn);\n\n?&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the above code, I have included a connection and import-controller file.<\/li>\n\n\n\n<li>Then, created the objects of both classes which are inside that file.<\/li>\n\n\n\n<li>I have passed the connection object in the class of <strong>ImportController<\/strong> as a parameter.<\/li>\n\n\n\n<li>Consequently, in the constructor of the ImportController class, we can get the connection object and furthermore, we can use the connection object as a global in that class.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/ajax-php-form-handling-using-jquery\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Ajax PHP Form Handling Using jQuery without Form Refresh (opens in a new tab)\">Ajax PHP Form Handling Using jQuery without Form Refresh<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Create_ImportController_File\"><\/span>Create ImportController File<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the project directory, create a new folder with name <strong>controllers <\/strong>and inside this folder create a new file named <code>import-controller.php<\/code>.<\/li>\n\n\n\n<li>In the next step, simply paste the below code in the <strong>import-controller.php<\/strong> file.<\/li>\n<\/ul>\n\n\n\n<pre title=\"import-controller.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\t\n\tclass ImportController {\n\t\t\/\/ getting connection in constructor\n\t\tfunction __construct($conn) {\n\t\t\t$this-&gt;conn \t\t =\t\t $conn;\n\t\t}\n\n\t\t\/\/ function for reading csv file\n\t\tpublic function index() {\n        \t$fileName         =        \"\";\n\n        \t\/\/ if there is any file\n\t        if(isset($_FILES['file'])) {\n\t        \t\/\/ reading tmp_file name\n\t    \t\t$fileName     =        $_FILES[\"file\"][\"tmp_name\"];\n\t        }\n\n\t\t\t$counter          =        0;\t\n\t\t\t\/\/ if file size is not empty\n\t        if (isset($_FILES[\"file\"]) &amp;&amp; $_FILES[\"file\"][\"size\"] &gt; 0) {   \n\t\t        $file       =  fopen($fileName, \"r\");\t    \n\t\t        \/\/ eliminating the first row of CSV file\n\t\t\t    fgetcsv($file);  ?&gt;\n\n\t\t\t    &lt;table class=\"table\"&gt;\n\t\t\t    \t&lt;thead&gt;\n\t\t\t    \t\t&lt;th&gt; Sl &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; Product Name &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; SKU &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; Brand &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; Quantity &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; Price Per Unit &lt;\/th&gt;\n\t\t\t    \t\t&lt;th&gt; Response &lt;\/th&gt;\n\t\t\t    \t&lt;\/thead&gt;\n\t\t\t        \n\t\t        &lt;?php \n\t\t        \twhile (($column = fgetcsv($file, 10000, \",\")) !== FALSE) { \n\t\t\t            $counter++;\t   \n\t\t\t            \/\/ assigning csv column to a variable\n\t        \t \t\t$product_name      =       $column[0];\n\t        \t \t\t$sku         \t   =       $column[1];\t        \t \t\t\n\t        \t \t\t$brand             =       $column[2];\t        \t \t\t\n\t        \t \t\t$quantity\t\t   =\t   $column[3];        \t \t\t\n\t        \t \t\t$price_per_unit    =\t   $column[4];\t                \n\n\t                \t\/\/ inserting values into the table\n\t                \t$sql \t\t\t\t=\t\t\"INSERT INTO products (product_name, sku, brand, quantity, price_per_unit) VALUES ('$product_name', '$sku', '$brand', '$quantity', '$price_per_unit') \";\n\t                \t$result \t\t\t=\t\t$this-&gt;conn-&gt;query($sql);\n\n\t                \tif($result == 1): ?&gt;                \t\t\n\t                    \t\t&lt;tr&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $counter; ?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $product_name; ?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $sku; ?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $brand; ?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $quantity; ?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo $price_per_unit; ?&gt; &lt;\/td&gt;\n\t     \t\t\t\t\t\t\t&lt;td&gt; &lt;?php echo \"&lt;label class='text-success'&gt;Success &lt;\/label&gt; \" .date('d-m-Y H:i:s');?&gt; &lt;\/td&gt;\n\t\t\t\t\t\t\t\t&lt;\/tr&gt;\n\t                \t&lt;?php endif;\n\t\t\t\t}\n\t\t\t\t ?&gt;\n\t\t\t\t&lt;\/table&gt;\n\t\t\t\t&lt;?php\n\t\t\t}\n\t\telse{\n\t\t}\n\t}\t\n}\n\n?&gt;<\/code><\/pre>\n\n\n\n<p>Finally, move to the <strong>index.php<\/strong> file and paste the single line of code before the closing of the main div, so that it will display the values in a table which will be inserting into the database.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;div class=\"row mt-4\"&gt;\n\t&lt;div class=\"col-md-10 m-auto\"&gt;\n\t\t&lt;?php\n\n\t\t\t$importResult   =  $importCtrl-&gt;index(); \t\n\t\t\t\t\t\t\t\t\n\t\t?&gt;\n\t&lt;\/div&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>Now, after the above code, the index.php file will become like this &#8211;<\/p>\n\n\n\n<pre title=\"index.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php \n\t\n\t\/\/ including db config file\n\tinclude_once '.\/config\/db-config.php';\n\n\t\/\/ including import controller file\n\tinclude_once '.\/controllers\/import-controller.php';\n\n\t\/\/ creating object of DBController class\n\t$db              =    \tnew DBController();\n\n\t\/\/ calling connect() function using object\n    $conn            =    \t$db-&gt;connect();\n\n    \/\/ creating object of import controller and passing connection object as a parameter\n\t$importCtrl      =    \tnew ImportController($conn);\n\n?&gt;\n\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;Import CSV in PHP &lt;\/title&gt;\n\n\t&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css\"&gt;\n\n&lt;\/head&gt;\n&lt;body&gt;\n\t&lt;div class=\"container\"&gt;\n\t\t&lt;form method=\"post\" enctype=\"multipart\/form-data\"&gt;\n\t\t\t&lt;div class=\"row mt-5\"&gt;\n\t\t\t\t&lt;div class=\"col-md-6 m-auto border shadow\"&gt;\n\t\t\t\t\t&lt;label&gt; Import Data &lt;\/label&gt;\n\t\t\t\t\t\t&lt;div class=\"form-group\"&gt;\t\t\t\t\t\n\t\t\t\t\t\t\t&lt;input type=\"file\" name=\"file\" class=\"form-control\"&gt;\n\t\t\t\t\t\t&lt;\/div&gt;\n\t\t\t\t\t\t&lt;div class=\"form-group\"&gt;\n\t\t\t\t\t\t\t&lt;button type=\"submit\" name=\"import\" class=\"btn btn-success\"&gt; Import Data &lt;\/button&gt;\n\t\t\t\t\t\t&lt;\/div&gt;\t\t\t\t\n\t\t\t\t&lt;\/div&gt;\t\t\n\t\t\t&lt;\/div&gt;\n\n\t\t\t&lt;div class=\"row mt-4\"&gt;\n\t\t\t\t&lt;div class=\"col-md-10 m-auto\"&gt;\n\t\t\t\t\t&lt;?php\n\n\t\t\t\t\t\t$importResult   =  $importCtrl-&gt;index(); \t\n\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t?&gt;\n\t\t\t\t&lt;\/div&gt;\n\t\t\t&lt;\/div&gt;\t\n\t\t&lt;\/form&gt;\n\t&lt;\/div&gt;\n\n\n\t&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.2.1.slim.min.js\"&gt;&lt;\/script&gt;\n\t&lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.12.9\/umd\/popper.min.js\"&gt;&lt;\/script&gt;\n\t&lt;script src=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/js\/bootstrap.min.js\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Save and run the project to see the result. Select the CSV file and click on the Import Data button.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"250\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/Import-CSV-in-PHP.png?resize=652%2C250&#038;ssl=1\" alt=\"read csv file in php\" class=\"wp-image-582\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/Import-CSV-in-PHP.png?w=652&amp;ssl=1 652w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/Import-CSV-in-PHP.png?resize=300%2C115&amp;ssl=1 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-element-caption\"><strong>Choose CSV File<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Here, you can see the data has been imported successfully and the records which have been inserted into the database has displayed before inserting.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"628\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-imported-data-1024x628.png?resize=1024%2C628&#038;ssl=1\" alt=\"read csv file in php\" class=\"wp-image-581\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-imported-data.png?resize=1024%2C628&amp;ssl=1 1024w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-imported-data.png?resize=300%2C184&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-imported-data.png?resize=768%2C471&amp;ssl=1 768w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/CSV-imported-data.png?w=1064&amp;ssl=1 1064w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<\/div>\n\n\n<p>Even more, you can see in the database table, the records have been inserted successfully.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"359\" src=\"https:\/\/i0.wp.com\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/database-table.jpg?resize=947%2C359&#038;ssl=1\" alt=\"CSV imported data\" class=\"wp-image-583\" srcset=\"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/database-table.jpg?w=947&amp;ssl=1 947w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/database-table.jpg?resize=300%2C114&amp;ssl=1 300w, https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/database-table.jpg?resize=768%2C291&amp;ssl=1 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><figcaption class=\"wp-element-caption\">Products Table in the Database<\/figcaption><\/figure>\n<\/div>\n\n\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link has-very-light-gray-color has-text-color has-background wp-element-button\" href=\"https:\/\/www.programmingfields.com\/wp-content\/uploads\/2019\/09\/csv-importer.zip\" style=\"background-color:#0b0c31\">Download Project<\/a><\/div>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/www.programmingfields.com\/datatable-in-php-with-mysql-database\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Implement jQuery DataTable in PHP MySQL Database (opens in a new tab)\">Implement jQuery DataTable in PHP MySQL Database<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Finally, we have learned how to upload a<strong> CSV file in PHP<\/strong>. Also, we have stored the values into the database table of the uploaded <a href=\"https:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"CSV (opens in a new tab)\">CSV<\/a> file. I hope, this technique will help you a lot in such type of situation when you will have to insert more than one data at a time. For the reason, that single-single record insertion is the time taking process. So, don&#8217;t forget to ask through the comment if you find any problem or query in this post. I will be glad to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can import the CSV files in PHP and store it into the database. The CSV (Comma-Separated Value) data can be imported easily using PHP Import CSV procedure. When we have a bunch of data to insert it into the database, then, in that case, inserting records one by one is very time-consuming. So to [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":591,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[306,390,389,304,391],"yst_prominent_words":[134,352,353,375,18,384,377,361,370,235,23,26,262,360,387,388,376,374,380,310],"class_list":{"0":"post-525","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-php","8":"tag-file-upload-in-php-mysql","9":"tag-import-csv-file-in-php","10":"tag-php-csv-upload","11":"tag-php-file-upload-script","12":"tag-read-csv-file-in-php","13":"entry"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/programmingfields.com\/wp-content\/uploads\/2019\/09\/Import-CSV-File-data-in-PHP-MysQL-Database.png?fit=560%2C315&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/comments?post=525"}],"version-history":[{"count":2,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"predecessor-version":[{"id":8974,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/posts\/525\/revisions\/8974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media\/591"}],"wp:attachment":[{"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/tags?post=525"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/programmingfields.com\/wp-json\/wp\/v2\/yst_prominent_words?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}