importing csv with mongodb from php code example

Example: mongodb uploading csv php

<?php

$mongo = new Mongo("mongodb://192.168.1.7:27017");

$collection = $mongo->test->cartoons;
$row = 1;
 $handle = fopen(dirname(Yii::app()->request->scriptFile).'/images/importcsv/upload.csv', "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
       echo "<p> $num fields in line $row: <br /></p>\n";
       $row++;
       $users = array();
       for ($c=0; $c < $num; $c++) {
            echo 'data--'.$data[$c] . "<br />\n";
        }
       $users[] =$data;
       var_dump($users);
       $collection->batchInsert($users);
    }
?>

Tags:

Php Example