0

I have an ANSI encoded CSV file and I need to change the encoding to UTF-8 when I upload the file to a server.

I'm using cakephp 2.x I need this for correct read row by row my csv file.

function import() {
    if ($this->request->is('post') || $this->request->is('put')) {
        $file = $this->request->data['Document']['submittedfile'];

        // NOW CHANGE ENCODING - but how ?
        // $this->pdfadd1->save($this->request->data);$filename = TMP . 'uploads' . DS . 'Address' . DS . $filename;

        move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'],     TMP . 'uploads' . DS . 'Good' . DS . "towary.csv");
        $messages = $this->Good->import("towary.csv");
        $this->set('messages', $messages);
    }
hobs
  • 18,473
  • 10
  • 83
  • 106
kol1991
  • 65
  • 3
  • 13
  • mb_* functions should help you out, namely [mb_convert_encoding](http://www.php.net/manual/en/function.mb-convert-encoding.php) – Mikk Jul 02 '14 at 22:15

1 Answers1

1

Try this:

$encodedOutput = mb_convert_encoding($file, 'Windows-1252', 'UTF-8');

Referring to this answer:

ANSI encoding is a slightly generic term used to refer to the standard code page on a system, usually Windows. It is more properly referred to as Windows-1252 (at least on Western/U.S. systems, it can represent certain other Windows code pages on other systems).

John
  • 1
  • 13
  • 98
  • 177
ecnepsnai
  • 1,882
  • 4
  • 28
  • 56
  • But when do this I get: ERROR: mb_convert_encoding() expects parameter 1 to be string – kol1991 Jul 03 '14 at 10:44
  • The error is pretty self explanatory. The `$file` parameter in the `mb_convert_encoding` method needs to be a string object with the contents that you want to convert. – ecnepsnai Jul 03 '14 at 16:49
  • Thx for help. Up for up :) Here is example solution: $filename = TMP . 'uploads' . DS . 'Good' . DS . $filename; $file = file_get_contents($filename); $encodedOutput = mb_convert_encoding($file, "Windows-1252", "UTF-8"); file_put_contents($filename, $encodedOutput); – kol1991 Jul 03 '14 at 18:07
  • @John Your answer would convert from UTF-8 to Windows-1252. The format is mb_convert_encoding(string, to, [from]). ("From" is optional). hobs wanted to convert from "ANSI" to UTF-8, so the correct conversion is: $encodedOutput = mb_convert_encoding($file, 'UTF-8', 'Windows-1252'); –  Sep 21 '22 at 18:18