2

I had a script called CSVimporter V3 for PHP that I used to run on a website and it worked fine. A couple of years later I've now dug out the same script to use on another project, all works okay except the CSV files are being read as one long line, as opposed to header row and multiple lines.

Here is part of the script.

Any ideas why it would be being read as a long line?

<?php 

//  Reference session variable for short-hand
        $f = &$_SESSION['csv_file'];

        //  Open file - fp = file pointer
        if (!$fp = @fopen($f, 'r')) {

            error(L_ERROR_PREVIEW_NO_FILE);

        } else {

            //  Array to store each row to be inserted
            $batch = array();

            //  Row counter
            $rc = 0;

            //  Work out starting row
            switch ($_SESSION['csv_first_row']) {
                case 'data':
                    $start_row = 0;
                break;

                default:
                    $start_row = 1;
            }

            //  Get contents, while below preview limit and there's data to be read
            while ($data = fgetcsv($fp, 1024, delimiter_to_char($_SESSION['csv_delimiter']))) {

                if ($rc < $start_row) {

                    //  Incremement counter
                    $rc++;

                    //  Go to next loop
                    continue;

                } else {

                    //  Array to store data to be inputted
                    $values = array();

                    //  Loop data
                    for ($i = 0; $i < count($data); $i++) {

                        //  If data is wanted, put data into array
                        if (array_key_exists($i, $column_index)) {
                            $values[$column_index[$i]] = $data[$i];
                        }

                    }

                    //  Sort array into correct order by index
                    ksort($values);

                    //  Join values together and store in array
                    $batch[] = '("' . implode('", "', str_replace('"', '\"', $values)) . '","'.$accti.'","'.$impt.'")';

                }

            }


        }
            //  Close the file
            fclose($fp); 
4
  • what is the value of $_SESSION['csv_delimiter'] ? Commented Mar 14, 2013 at 10:51
  • Is $_SESSION['csv_delimiter'] being set? Try var_dump($_SESSION); and see if it exists Commented Mar 14, 2013 at 10:51
  • Yes ['csv_delimiter'] is being set earlier on, I've just condensed it for this site. The value is "," Commented Mar 14, 2013 at 10:57
  • The individual fields are being delimited, it's just when a new row is read in the CSV file it's being appended onto the end of the previous row which in effect creates one big row. Commented Mar 14, 2013 at 10:59

1 Answer 1

1

I added this at the top of the code and it all works now!

ini_set('auto_detect_line_endings', true);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.