5

I'm trying to save text file as UTF-8 by using Laravel's Storage facade. Unfortunately couldn't find a way and it saves as us-ascii. How can I save as UTF-8?

Currently I'm using following code to save file;

Storage::disk('public')->put('files/test.txt", $fileData);

she hates me
  • 1,212
  • 5
  • 25
  • 44

3 Answers3

5

You should be able to append "\xEF\xBB\xBF" (the BOM which defines it as UTF-8) to your $fileData. So:

Storage::disk('public')->put('files/test.txt", "\xEF\xBB\xBF" . $fileData);

There are other ways to convert your text before writing it to the file, but this is the simplest and easiest to read and execute. As far as I know, there is also no character encoding methods within Illuminate\Filesystem\Filesystem.

For more information: https://stackoverflow.com/a/9047876/823549 and What's different between UTF-8 and UTF-8 without BOM?.

1000Nettles
  • 2,314
  • 3
  • 22
  • 31
0

ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded. The bytes in the ASCII file and the bytes that would result from "encoding it to UTF-8" would be exactly the same bytes. There's no difference between them, so there's no need to do anything.

It looks like your problem is that the files are not actually ASCII. You need to determine what encoding they are using, and transcode them properly.

Mayank Majithia
  • 1,916
  • 16
  • 21
0

I recommend using mb_convert_encoding instead

$fileData = mb_convert_encoding($fileData, "UTF-8", "auto");
Storage::disk('public')->put('files/test.txt", $fileData);
gk.
  • 338
  • 5
  • 15