src/Model/Behavior/CsvBehavior.php
public function importCsv($content, $fields = array(), $options = array())
    {
        $config = $this->getConfig();
        $options = array_merge($config, $options);

        if (!$this->_trigger('beforeImportCsv', array($content, $fields, $options))) {
            return false;
        }

        if ($options['text']) {
            // store the content to a file and reset
            $file = fopen("php://memory", "rw");
            fwrite($file, $content);
            fseek($file, 0);
        } else {
            $file = fopen($content, 'r');
        }

        // open the file
        if ($file) {
            $data = [];

            if (empty($fields)) {
                // read the 1st row as headings
                $fields = fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure']);
                foreach ( $fields as $key => $field ) {
                    $field = trim($field);
                    if ( empty($field) ) {
                        continue;
                    }
                    $fields[$key] = strtolower($field);
                }
            } elseif ( $options['headers'] ) {
                fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure']);
            }
            // Row counter
            $r = 0;
            // read each data row in the file
            $alias = $this->_table->getAlias();
            while ($row = fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure'])) {
                // for each header field
                foreach ($fields as $f => $field) {
                    if (!isset($row[$f])) {
                        $row[$f] = null;
                    }
                    $row[$f] = trim($row[$f]);
                    // get the data field from Model.field
                    if (strpos($field,'.')) {
                        $keys = explode('.',$field);
                        if ( $keys[0] == $alias ) {
                            $field = $keys[1];
                        }
                        if ( !isset($data[$r]) ) {
                            $data[$r] = [];
                        }
                        $data[$r] = Hash::insert($data[$r], $field, $row[$f]);

                    } else {
                        $data[$r][$field] = $row[$f];
                    }

                }
                $r++;
            }

            // close the file
            fclose($file);

            $this->_trigger('afterImportCsv', array($data));

            // return the messages
            return $data;
        } else {
            return false;
        }
    }
public function importCsv($content, $fields = array(), $options = array())
    {
        $config = $this->getConfig();
        $options = array_merge($config, $options);

        if (!$this->_trigger('beforeImportCsv', array($content, $fields, $options))) {
            return false;
        }

        if ($options['text']) {
            // store the content to a file and reset
            $file = fopen("php://memory", "rw");
            fwrite($file, $content);
            fseek($file, 0);
        } else {
            $file = fopen($content, 'r');
        }

        // open the file
        if ($file) {
            $data = [];

            if (empty($fields)) {
                // read the 1st row as headings
                $fields = fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure']);
                foreach ( $fields as $key => $field ) {
                    $field = trim($field);
                    if ( empty($field) ) {
                        continue;
                    }
                    $fields[$key] = strtolower($field);
                }
            } elseif ( $options['headers'] ) {
                fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure']);
            }
            // Row counter
            $r = 0;
            // read each data row in the file
            $alias = $this->_table->getAlias();
            while ($row = fgetcsv($file, $options['length'], $options['delimiter'], $options['enclosure'])) {
                // for each header field
                foreach ($fields as $f => $field) {
                    if (!isset($row[$f])) {
                        $row[$f] = null;
                    }
                    $row[$f] = trim($row[$f]);
                    // get the data field from Model.field
                    if (strpos($field,'.')) {
                        $keys = explode('.',$field);
                        if ( $keys[0] == $alias ) {
                            $field = $keys[1];
                        }
                        if ( !isset($data[$r]) ) {
                            $data[$r] = [];
                        }
                        $data[$r] = Hash::insert($data[$r], $field, $row[$f]);

                    } else {
                        $data[$r][$field] = $row[$f];
                    }

                }
                $r++;
            }

            // close the file
            fclose($file);

            $this->_trigger('afterImportCsv', array($data));

            // return the messages
            return $data;
        } else {
            return false;
        }
    }
public function importCsv ( $ content , $ fields = array ( ) , $ options = array ( ) ) { $ config = $ this -> getConfig ( ) ; $ options = array_merge ( $ config , $ options ) ; if ( ! $ this -> _trigger ( 'beforeImportCsv' , array ( $ content , $ fields , $ options ) ) ) { return false ; } if ( $ options [ 'text' ] ) { // store the content to a file and reset $ file = fopen ( "php://memory" , "rw" ) ; fwrite ( $ file , $ content ) ; fseek ( $ file , 0 ) ; } else { $ file = fopen ( $ content , 'r' ) ; } // open the file if ( $ file ) { $ data = [ ] ; if ( empty ( $ fields ) ) { // read the 1st row as headings $ fields = fgetcsv ( $ file , $ options [ 'length' ] , $ options [ 'delimiter' ] , $ options [ 'enclosure' ] ) ; foreach ( $ fields as $ key => $ field ) { $ field = trim ( $ field ) ; if ( empty ( $ field ) ) { continue ; } $ fields [ $ key ] = strtolower ( $ field ) ; } } elseif ( $ options [ 'headers' ] ) { fgetcsv ( $ file , $ options [ 'length' ] , $ options [ 'delimiter' ] , $ options [ 'enclosure' ] ) ; } // Row counter $ r = 0 ; // read each data row in the file $ alias = $ this -> _table -> getAlias ( ) ; while ( $ row = fgetcsv ( $ file , $ options [ 'length' ] , $ options [ 'delimiter' ] , $ options [ 'enclosure' ] ) ) { // for each header field foreach ( $ fields as $ f => $ field ) { if ( ! isset ( $ row [ $ f ] ) ) { $ row [ $ f ] = null ; } $ row [ $ f ] = trim ( $ row [ $ f ] ) ; // get the data field from Model.field if ( strpos ( $ field , '.' ) ) { $ keys = explode ( '.' , $ field ) ; if ( $ keys [ 0 ] == $ alias ) { $ field = $ keys [ 1 ] ; } if ( ! isset ( $ data [ $ r ] ) ) { $ data [ $ r ] = [ ] ; } $ data [ $ r ] = Hash :: insert ( $ data [ $ r ] , $ field , $ row [ $ f ] ) ; } else { $ data [ $ r ] [ $ field ] = $ row [ $ f ] ; } } $ r ++ ; } // close the file fclose ( $ file ) ; $ this -> _trigger ( 'afterImportCsv' , array ( $ data ) ) ; // return the messages return $ data ; } else { return false ; } }
Import public function

@param string $conent filename or path to the file under webroot
@param array $fields to import
@param array $options to set
@return array of all data from the csv file in [Model][field] format
@author Dean Sofer
Import public function