PHP Import CSV File

Blogs ❯❯ PHP

Image could not load

PHP Import CSV File

File : csv_import.html

<html> <head> <title>PHP Import CSV File</title> </head> <body> <form action="./csv_import.php" method="POST" enctype=multipart/form-data> <p><input type="file" name="myfile" multiple></p> <p><button type="submit">Submit</button></p> </form> </body> </html>

File : csv_import.php

<?php // check if file is coming. if(isset($_FILES['myfile'])) { //first open file in READ mode. if (($handle = fopen($_FILES['myfile']['tmp_name'], "r")) !== FALSE) { //here we will get every row as an array. $csv_data = []; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); if($num > 0) { // push row in an array. $csv_data[] = $data; } } // close file fclose($handle); // now print data. echo "<pre>"; print_r($csv_data); echo "</pre>"; } else { echo "File is not readable"; } } ?>

Explain

fopen()

fopen() function का use file को open करने के लिए किया जाता है , example में file को r mode के साथ file को open किया गया है क्योंकि यहां हमें सिर्फ file को read करना है। आप अपनी need के according file को दुसरे mode में भी open कर सकते हैं।

fgetcsv()

fgetcsv() function का use open CSV file की line को read करने के लिए use किया जाता है। यह function csv file की single line को separator (by default ,) और enclosure (by default ") के bases पर array return करता है।

fclose()

यह function open file pointer को close करता है।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook