{"id":2602,"date":"2024-12-31T20:23:15","date_gmt":"2024-12-31T20:23:15","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/how-to-read-csv-file-using-root-script-in-magento-2\/"},"modified":"2025-03-17T08:55:02","modified_gmt":"2025-03-17T08:55:02","slug":"read-csv-file-using-root-script-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/read-csv-file-using-root-script-in-magento-2\/","title":{"rendered":"How to Read CSV File Using Root Script in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Complete tutotial on using&nbsp;<em><strong>root script in Magento 2 to read data from CSV file<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are a Magento 2 developer, sometimes you may require to process records from a CSV file and add it to the Magento 2 database. In such case, you can use the&nbsp;<em><strong>Magento 2 root script to read data from the CSV file<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an example, consider you have thousands of rows of data that you want to import into Magento 2. In such scenario, adding the data manually can be tedious and therefore you may require to use the Magento 2 root script to read the data from the CSV file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s go with the root script to read CSV file data in Magento 2!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Read CSV Data in Magento 2 Using Root Script?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to read the data from a CSV file in Magento 2, we\u2019ll be using the&nbsp;<code>fopen<\/code>&nbsp;&amp;&nbsp;<code>fgetcsv<\/code>&nbsp;functions in the root script. After opening the file, we\u2019ll read each element of the file using the \u2018for loop\u2019 and close the file using the&nbsp;<code>fclose<\/code>, once the data reading is completed.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> &lt;?php use Magento\\Framework\\App\\Bootstrap; require 'app\/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager();\n$state = $objectManager->get('\\Magento\\Framework\\App\\State');\n$state->setAreaCode('frontend');\n \n$open = fopen(\"my_csv.csv\", \"r\"); \/\/ name of csv file\n$data = fgetcsv($open, 1000, \",\");\n \ntry{\n $handle = fopen(\"my_csv.csv\", \"r\"); \/\/ name of csv file\n for ($i = 0; $data = fgetcsv($handle); ++$i) {\n if ($i == 0) {\n continue;\n }\n echo $data[$i];\n }\n fclose($handle);\n}catch (\\Exception $e) {\n    print($e->getMessage());\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is how we can use the&nbsp;<em><strong>Magento 2 root script to read CSV file data<\/strong><\/em>. I hope this blog post will help through the same. In case you still have any queries or doubts, feel free to comment. I will be happy to help you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this Magento 2 tutorial with your Magento friends via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/get-data-from-custom-database-table-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Get Data From Custom Database Table in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/run-direct-sql-query-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How To Run Direct SQL Query In Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/download-all-product-data-in-csv-using-root-script-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Download All Product Data in CSV Using Root Script in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Complete tutotial on using&nbsp;root script in Magento 2 to read data from CSV file. If you are a Magento 2 developer, sometimes you may require&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2602","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2602"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2602\/revisions"}],"predecessor-version":[{"id":9495,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2602\/revisions\/9495"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}