php - How to find out the character set of a user uploaded document? -


i wrote script allows users upload/import lots of users @ once using csv-file. i'm using mysql's load data local infile make working:

$query = "load data local infile $file table my_table  fields terminated $delimiter  lines terminated '\\n'  (email, name, organization); 

but user tried import document contained name günther. saved database "g" (cutting of rest). document turned out in latin1 causing problems. don't want bother users character sets , stuff.

i know character set option supported load data local infile. but, though don't error when put character set latin1 in query, want utf-8. , happens if users uses file that's neither in utf-8 or latin1?

so how found out in character set user uploaded document , how convert utf-8?

you can find character encoding using mb_detect_encoding before running $query. detect encoding before load file.

suppose file name in $str

here basic example might help.

<?php /* detect character encoding current detect_order */ echo mb_detect_encoding($str);  /* "auto" expanded according mbstring.language */ echo mb_detect_encoding($str, "auto");  /* specify encoding_list character encoding comma separated list */ echo mb_detect_encoding($str, "jis, eucjp-win, sjis-win");  /* use array specify encoding_list  */ $ary[] = "ascii"; $ary[] = "jis"; $ary[] = "euc-jp"; echo mb_detect_encoding($str, $ary); ?> 

here link php.net's mb_detect_encoding

this work-around , heuristic way. make sure handle exceptions might incure (which might tedious, guess)

there class written might suite requirement (haven't tested code) on phpclasses.org


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -