Skip to content

Incompatibility with PHP 7.4 line 52 src/Util/Str.php #507

Closed

Description

@maietta

ERROR 500 - Internal Server Error
substr() expects parameter 1 to be string, bool given [/var/www/html/vendor/vlucas/phpdotenv/src/Util/Str.php:52]        

[vendor/vlucas/phpdotenv/src/Util/Str.php:52] substr()
[vendor/vlucas/phpdotenv/src/Store/File/Reader.php:76] Dotenv\Util\Str::utf8()
[vendor/phpoption/phpoption/src/PhpOption/Some.php:108] Dotenv\Store\File\Reader::Dotenv\Store\File\{closure}()
[vendor/vlucas/phpdotenv/src/Store/File/Reader.php:79] PhpOption\Some->flatMap()
[vendor/vlucas/phpdotenv/src/Store/File/Reader.php:48] Dotenv\Store\File\Reader::readFromFile()
[vendor/vlucas/phpdotenv/src/Store/FileStore.php:62] Dotenv\Store\File\Reader::read()
[vendor/vlucas/phpdotenv/src/Dotenv.php:222] Dotenv\Store\FileStore->read()
[global.php:32] Dotenv\Dotenv->load()
[process_tables.php:3] require('/var/www/html/global.php')

To fix this, I changed line 52  in /src/Util/Str.php from:

    if (\substr($converted, 0, 3) == "\xEF\xBB\xBF") {

to 

    if (\substr("$converted", 0, 3) == "\xEF\xBB\xBF") {

That appears to have "fixed" my problem.

Activity

  1. GrahamCampbell commented on Dec 18, 2021

    @GrahamCampbell
    Collaborator

    Oh, crap. Thanks for pointing this out, introduced in #501. This happens when decoding fails. I'll adjust the code so the proper error is bubbled up for you. I think the change you made will instead result in the .env file being thrown away.

  2. maietta commented on Dec 18, 2021

    @maietta
    Author

    Thank you!

  3. GrahamCampbell commented on Dec 18, 2021

    @GrahamCampbell
    Collaborator

    What encoding were you trying to use btw? My "fix" will just make your app crash with an error saying "UTF-8 decoding failed." instead.

  4. maietta commented on Dec 18, 2021

    @maietta
    Author

    What encoding were you trying to use btw? My "fix" will just make your app crash with an error saying "UTF-8 decoding failed." instead.

    Not sure, just plain text I suppose. I know it's UTF-8 though. No weird characters or BOM's introduced.

  5. GrahamCampbell commented on Dec 18, 2021

    @GrahamCampbell
    Collaborator

    What's happening is mb_convert_encoding($input, 'UTF-8') is returning false on your machine.

  6. GrahamCampbell commented on Dec 18, 2021

    @GrahamCampbell
    Collaborator

    You could try installing ext-mbstring. We use a symfony provided polyfill if the extension is missing, which may contain bugs. Using the extension may fix this issue for you.

  7. maietta commented on Dec 18, 2021

    @maietta
    Author

    I ran composer require ext-mbstring and that installed fine. Same error.

    My .env file looks like this:

    PMA_HOSTNAME = "phpmyadmin.localhost"
    MYSQL_DATABASE = m00000685
    MYSQL_USER = m00000685
    MYSQL_PASSWORD = redacted
    MYSQL_ROOT_PASSWORD = redacted
    MYSQL_PORT = 3306
    

    Don't know if that helps.

    The PHP loader looks like this:

    <?php
    
    require_once("/var/www/html/vendor/autoload.php");
    $env = Dotenv\Dotenv::createImmutable("/var/www");
    $env->load();
    extract($_ENV);
    $dsn = "mysql:host=mysql;dbname=${MYSQL_DATABASE};port=${MYSQL_PORT};charset=utf8mb4;";
    ...
    
  8. GrahamCampbell commented on Dec 18, 2021

    @GrahamCampbell
    Collaborator

    It is not possible to install PHP extensions using composer. It's likely the extension is still not installed.

    Can you run this code on your server:

    <?php
    
    var_dump(function_exists('mb_convert_encoding'));
  9. maietta commented on Dec 18, 2021

    @maietta
    Author

    I found the php7-mbstring package for Alpine Linux, 3.15.0 which is what I am building for my lightweight Nginx + PHP-FPM container which my application runs in.

    I've gone ahead and added the Alpine php7-mbstring package so now that core function made available.

    After doing this, the problem went away. This is not a bug in your software. This was the lack of the proper core PHP function being available after upgrading my container to PHP 7.4.

    Thank you for helping me pinpoint this.

  10. eduardomart commented on Oct 22, 2022

    @eduardomart

    I am also getting Internal Server Error when trying to load dotenv in a production server.
    If I comment out the following two lines, the internal server error goes away, so I know it has to do with dotenv

    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->safeload();
    

    I tried your suggestion and ran var_dump(function_exists('mb_convert_encoding')); and I get: bool(true)

    also tried maietta's suggestion in the first post, but it made no difference in my case.

    I am running the same php version locally and on my production server (7.4.32)

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