File reading is not working properly in php [Updated]

Scenario: Migrating from Existing environment to new environment (Both are Linux environments) for PHP CodeIgnitor application. While accessing the application it is found that file_get_contents (and read_file also) is not reading 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 file_get_contents. 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 "
Content is $content
"; 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).

display_errors=On 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

  2. Checked to read the file using readfile ,but still the same behavior.

  3. Checked whether the file is readable just before the ‘file_get_contents’ code and it is returning true
    if(is_readable($file)) {echo ("$file is readable");} else {echo ("$file is not readable");}

  4. var_dump($content) is returning null.

I suspect it is point to the wrong folder address. First, on a server, you set up your WWW folder and your code is inside of that. Most direct folders in the server are protected. You can sometimes alter the permissions on servers to allow reading files.

Also, PHP files have trouble reading files that start with a period. This is usually a trick to not allow direct reading of the files. Does it work for other files in that folder? If it is working in one system and not the other, it is most likely the folder or file permissions.

file_get_contents will return false if it cannot read the file. Instead of echo "Content is $content"; try var_dump($content); to see the returned value.

The function will also throw a warning if it cannot read the file; this warning will give you more information about what is wrong. If the warning is not printed to your output, check your PHP ini settings to make sure it is logging errors.

This is not working for any files and also I checked with another library ‘readfile’ which is also having the same issue.So as per this ,file reading operations are not working

Well, how have you debugged this so far? First test would be to have the test page load a file in the same folder just to make sure your PHP installation works. Just create a simple text file and put it into the test page’s folder. Then, test loading it directly in a test file. If that works, then, move the file to another folder and see if you can load it from that folder. Finally, move it to the server folder you posted above and test a third time. You need to find out if it is a PHP problem, a folder/file permissions problem or another code problem.

We can not duplicate your server to help you. If it works on one server and not another, there has to be some difference in you php.ini file, folder/file permissions or other things that is causing the issue.

Another work-around would be to just use cURL instead. cURL has extensive error tracking routines that might give you better control over loading the file. But, as Skawid mentioned, look at your server logs especially in the PHP log sections. They should tell you what you need to know to fix it.

Thanks @ErnieAlex @skawid.
Made the below sample php and make it available in my current server configuration. This is available by accessing through url and php is getting executed properly.
ini_set(‘display_errors’, 1); // show errors
//set your own error handler before the call
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );

echo “fopen url is”;
$file =‘input.txt’;

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 , following are the points:
1.When accessing through the apache server (i.e. through url for e.g. https://myapplicationdomain/myapplication/above.php). Currently apache_user is having all permissions for the file. In the above case,text file is placed in the same directory where php is present
a. is_readable is returning ‘true’
b. file_exists is returning ‘true’
c. file_get_contents is returning blank and when I did var_dump, it is returning ‘Null’
d. No errors are getting logged in this case.
e. Changed the permission of the ‘input.txt’ to see the permission behaviour. In that case,proper error got displayed in is_readable it self.
So through this way ruled out the possibility of permission issue.
2. As mentioned if I run the above snippet through php sample.php.This is working as expected.
3. Checked the following php ini files and confirmed disabled functions also.
open_basedir : novalue
disabled_functions: no value

Did you create the input.txt file with some data inside it? Assuming it does, you should take the assignment of the filename outside of the TRY. Place $file=‘input.txt’ line above the try to insure that is is assigning the value to it and not removing it if the TRY fails.
Also, you are using a try without a catch and no throw if the try fails. Confused on your logic there.

One other thing. Which version of PHP are you using?

I can’t think of a way for file_get_contents to return null. What does the following code output?

ini_set('display_errors', 1);

Yes,I know ,it seems to be strange behavior. The above snippet I executed and it returns

Ok. What about if you point at a non existent file?

ini_set('display_errors', 1);

:slight_smile: Actually for the same input file, I checked is_readable and file_exists in the same php which is returning true .

Sponsor our Newsletter | Privacy Policy | Terms of Service