Reputation: 868
After a deep research in the forum, I am posting my problem here, because no topic matched my situation.
I am importing a file (csv or Excel) in Laravel, in my controller I am using Input::file('file_name')
to get the file.
The user has to possibility to choose his encoding from a select in the interface.
So my problem, is that I want to change the file encoding, to that set by the user.
I used mb_detect_encoding
function but I always if I check after, I have the ASCII encoding always...
Here my code:
$encoding = Input::get('encoding');
$fileContent = \File::get($importFile);
$importFile = Input::file('import_file');
$enc = mb_detect_encoding($fileContent , mb_list_encodings(), true);
if ($enc !== $encoding){
\File::put($importFile,mb_convert_encoding(\File::get($importFile), $encoding, $enc));
}
Upvotes: 1
Views: 3104
Reputation: 14520
According to the docs for mb_detect_encoding($str)
:
Detects character encoding in string str
And according to the Laravel 5.1 docs for file uploads:
The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class
So in your code above, $importFile
is an instance of a class. Passing that into mb_detect_encoding
won't give you the encoding of the file that instance represents.
To check the encoding of the file's content, you need to load that content first:
$importFile = Input::file('import_file');
$fileContent = file_get_contents($importFile->path());
Then you can pass the content to mb_detect_encoding()
and check the encoding:
$enc = mb_detect_encoding($importFile, mb_list_encodings(), true);
Upvotes: 1