Skip to content

Add support for BOM-signed env files #500

Closed

Description

@hosni

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.

image

image

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".

Activity

  1. GrahamCampbell commented on Nov 8, 2021

    @GrahamCampbell
    Collaborator

    Yes, this is supported, but you need to tell us the encoding. https://github.com/vlucas/phpdotenv/blob/master/src/Dotenv.php#L83. :)

  2. hosni commented on Nov 8, 2021

    @hosni
    ContributorAuthor

    Yes, this is supported, but you need to tell us the encoding. https://github.com/vlucas/phpdotenv/blob/master/src/Dotenv.php#L83. :)

    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!

    Note that UTF-8 is different that UTF-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-BOM and test-utf8-with-boom.php into root of the package and then run:
    php test-utf8-with-boom.php

  3. hosni commented on Nov 8, 2021

    @hosni
    ContributorAuthor

    I created a pull request for this issue: #501

  4. GrahamCampbell commented on Nov 8, 2021

    @GrahamCampbell
    Collaborator

    Thanks. I'll take a look soon. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions