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);
Advertisement
Answer
You should be able to append “xEFxBBxBF” (the BOM which defines it as UTF-8) to your $fileData
. So:
Storage::disk('public')->put('files/test.txt", "xEFxBBxBF" . $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 IlluminateFilesystemFilesystem
.
For more information: https://stackoverflow.com/a/9047876/823549 and What’s different between UTF-8 and UTF-8 without BOM?.