You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I was working on a Laravel project that has a .env file.
But I faced an error that says: The environment file is invalid! Failed to parse dotenv file due to an invalid name. Failed at [APP_NAME].
After some time for debugging, I realized the problem is come from encoding of the file!
The encoding of the .env file is UTF8 BOM
In this example files you will see two files named .env-UTF-8 and .env-UTF-8-with-BOM. They almost contains the same data, but .env-UTF-8-with-BOM starts with the UTF8 BOM, causing the parse error.
How did happen?
I'm using linux as OS, but well it seems that when you add a text file in Visual Studio in windows, it adds the UTF8 BOM by default. I generated the second file that has to BOM using the Windows Explorer contextual menu "New File".
I tell the encoding and use UTF-8, But I got Dotenv\Exception\InvalidFileException: Failed to parse dotenv file. Encountered an invalid name at [FOO]. error!
I also tried ALL encoding that mb_list_encodings return, but NONE of them works!
Open directory: add-support-for-BOM-signed-env-files
Please copy .env-UTF-8-with-BOM and test-utf8-with-boom.php into root of the package and then run: php test-utf8-with-boom.php
Activity
GrahamCampbell commentedon Nov 8, 2021
Yes, this is supported, but you need to tell us the encoding. https://github.com/vlucas/phpdotenv/blob/master/src/Dotenv.php#L83. :)
hosni commentedon Nov 8, 2021
I tell the encoding and use
UTF-8, But I gotDotenv\Exception\InvalidFileException: Failed to parse dotenv file. Encountered an invalid name at [FOO].error!I also tried ALL encoding that
mb_list_encodingsreturn, but NONE of them works!Note that
UTF-8is different thatUTF-8 with BOM!I write a simple code to test it, please see test-utf8-with-boom.php
Open directory: add-support-for-BOM-signed-env-files
Please copy
.env-UTF-8-with-BOMandtest-utf8-with-boom.phpinto root of the package and then run:php test-utf8-with-boom.phphosni commentedon Nov 8, 2021
I created a pull request for this issue: #501
GrahamCampbell commentedon Nov 8, 2021
Thanks. I'll take a look soon. :)