file reading is not working properly in php

Posted November 30, 2020 2.6k views
PHPPHP Frameworks

Migrating from Existing environment to new environment (Both are Linux environments) for PHP CodeIgnitor application.
While accessing the application it is found that filegetcontents is not fetching values from the environment file.
The application is running on an apache web server and application has to read the variables from the environment file using filegetcontents. But it is returning the blank value and not throwing any errors.

Php Version: 7.3.5

Framework used is CodeIgnitor and also using php-fpm

Code: try{$content = file_get_contents('/samplepath/myapplication/.env');echo " <br/> Content is $content <br/>"; catch (Exception $e) {echo 'Error Caught $e';}

Expected Result : This should return all the values from .env file

Actual Result: It is returning blank while accessing through application hosted in apache web server


  1. When executed the above code independently, this is fetching the values properly from the environment file.
    i.e. if above code snippet pasted to a test.php and execute php test.php >> This is working as expected

  2. The path provided in the above file is an absolute path. (above path is a sample path given)

  3. Gave full permission to the user to access the file.

  4. Checked with a different file (other than .env)also which is showing the same behavior.

  5. Made the following changes in php.ini also. Not able to see any error in logs (checked in server logs and fpm logs).

error_reporting= E_ALL & ~E_DEPRECATED & ~E_STRICT

  1. Not sure whether this is related as the below configuration is related to URL, but still it is enabled allow_url_fopen = On

7.Checked to read the file using readfile ,but still the same behavior as it is returning blank

  1. Checked whether the file is readable just above the read code and it is returning true

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi @vineeth604,

Reading from all of it, it does sound like wrong permissions or ownership. You mentioned you have the user full permissions to that file however, having said that, Permissions are inherited from the folder they are in as well. Did you change the permissions of your website’s folder?

One other thing while talking about permissions, is ownership. Did you check your files ownership? This is the most common problem for not being enable to execute,open,read a file. Most probably the ownership of the folders and files is set to root:root while it should be the one that Apache uses.


  • Hi @KFSys I confirmed the permissions of the directory and files. Actually It is in place. This seems to be strange issue as you can see the below code.

    'if(is_readable($file)) {
      echo "$file is readable \n";
    } else {
      echo "$file is not readable \n";
    $homepage = file_get_contents($file);
    echo "content is $homepage  \n";
    echo "file is not present \n";
    }catch (Exception $e) {echo 'Error Caught $e';}

    So in the above case both isreadable and fileexists are working as expected (returning true) while accessing the above php through server. When I changed the permission of input.txt, is_readable is returning false. So that is why I ruled out the possibility of permission.

    edited by KFSys
    • Hi @vineeth604

      Try to run this .php code file in your browser. Executing PHP from your server with the root user and executing it from the browser with another user will bring out different results.

      See if it would work that way.