Monday 10 December 2012

Import csv file in PHP


Import data from csv/xls file is a main requirement in most of the web applications. We can do this simply by using the fgetcsv function in PHP. Here is the code for it.

<?PHP
 
// Import csv - PHP 
 
$row = 1;  
 
if (($handle = fopen("test.csv", "r")) !== FALSE) {  
 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {  
     
        $num = count($data);
         
        echo "<p> $num fields in line $row: <br /></p>\n";
         
        $row++;
         
        for ($c=0; $c < $num; $c++) {
         
            echo $data[$c] . "<br />\n";
             
        }
         
    }
     
    fclose($handle);
     
}
 
?>
Its easy and so we can complete in a few steps.

Try it !


No comments:

Post a Comment