Handling csv files fgetcsv & str_getcsv?

I have a three-part question: fgetcsv is better than str_getcsv , and is there a way to allow .csv file types to be displayed in the file download dialog? In the latter case, I need to use ini_set ('auto_detect_line_endings', true)

<?php if(isset($_POST['submit'])) { //$filename=$_POST['filename']; $filename = file_get_contents($_FILES['uploadedfile']['tmp_name']); $handle = fopen("$filename", "r"); while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { $import="INSERT into kmmb_member1(no_ahli,no_pin,nama,no_ic_baru,no_ic_lama) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')"; mysql_query($import) or die(mysql_error()); } fclose($handle); print "Import done"; } else { print "<form action='import.php' method='post'>"; print "Type file name to import:<br />"; // print "<input type='text' name='filename' size='20' /><br />"; print "Select csv file: <input name='uploadedfile' type='file' /><br />"; print "<input type='submit' name='submit' value='submit' /></form>"; } ?> 
+6
php
source share
1 answer

Is fgetcsv() better than str_getcsv() ?

Yes, when opening from a file. Use str_getcsv() only if the CSV already has a line in your program.

Can I show .csv file types only in the file download dialog?

Not unless you use a Flash wrapper . However, you can detect the file extension using JavaScript, and you should only accept .csv with PHP (use pathinfo($filename, PATHINFO_EXTENSION) ).

However, by guaranteeing that it has .csv , it does not confirm that it is actually a CSV file.

Should I / I use ini_set('auto_detect_line_endings', true);

Not if it does not work. There is a slight performance limitation in the docs document when using it. Therefore, use it only when necessary. It is also disabled by default. It is better to leave the less common stuff in php.ini by default, I found.

Although make sure that magic_quotes and register_globals always off :)

+10
source share

All Articles