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 ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook