-1

I got the following code which actually works for a comma separated csv file. But due to the fact im located in Germany I need semicolon separated import. Can anyone tell me where I can change this in my code?

Thanks a lot!

if(isset($_POST['importSubmit'])){

    //validate whether uploaded file is a csv file
    $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){

            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');

            //skip first line
            fgetcsv($csvFile);

            //parse data from csv file line by line
            while(($line = fgetcsv($csvFile)) !== FALSE){
                    $connection->query("INSERT INTO adressen (name, email, status) VALUES ('".$line[0]."','".$line[1]."','".$line[2]."')");

            }

            //close opened csv file
            fclose($csvFile);

            $qstring = '?status=succ';
        }else{
            $qstring = '?status=err';
        }
    }else{
        $qstring = '?status=invalid_file';
    }
}
Matt
  • 1,518
  • 4
  • 16
  • 30
  • 1
    Possible duplicate of [Save CSV files into mysql database](https://stackoverflow.com/questions/11432511/save-csv-files-into-mysql-database) – PrakashG Feb 13 '19 at 09:31
  • Can you explain where exactly a problem occurs? What **exactly** does not work when you use a different CSV format? – Nico Haase Feb 13 '19 at 09:33

2 Answers2

1

You can put a delimiter where you want to break on. Please look at the php documentation it is a old function with lots of help on it http://php.net/manual/en/function.fgetcsv.php

Eningly
  • 73
  • 1
  • 6
0

You can use the LOAD DATA LOCAL INFILE which has separator options to load directly from the local filesystem into a table.

You will need to enable PDO::MYSQL_ATTR_LOCAL_INFILE when you connect to MySQL.

danblack
  • 12,130
  • 2
  • 22
  • 41