69

I am getting the following error from Apache

[Sat Mar 19 23:10:50 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required '/common/configs/config_templates.inc.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/viapics1/public_html/common/configs/config.inc.php on line 158

I am definately not an expert of Apache but the file config.inc.php & config_templates.inc.php are there. I also tried navigating to a test.html page I placed in common/configs/ so I assume there is no rights issues going on. I also set the rights on config_templates.inc.php to give everyone read, write, and execute rights. Not sure what to do at this point, I checked to see if there was a /usr/share/php directory and I found there was not but when I did yum install php it said it had the latest. Ideas?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Al Katawazi
  • 7,192
  • 6
  • 26
  • 39

7 Answers7

109

It's not actually an Apache related question. Nor even a PHP related one. To understand this error you have to distinguish a path on the virtual server from a path in the filesystem.

require operator works with files. But a path like this

                          /common/configs/config_templates.inc.php

only exists on the virtual HTTP server, while there is no such path in the filesystem. The correct filesystem path would be

/home/viapics1/public_html/common/configs/config_templates.inc.php

where

/home/viapics1/public_html

part is called the Document root and it connects the virtual world with the real one. Luckily, web-servers usually have the document root in a configuration variable that they share with PHP. So if you change your code to something like this

require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php';

it will work from any file placed in any directory!

Update: eventually I wrote an article that explains the difference between relative and absolute paths, in the file system and on the web server, which explains the matter in detail, and contains some practical solutions. Like, such a handy variable doesn't exist when you run your script from a command line. In this case a technique called "a single entry point" is to the rescue. You may refer to the article above for the details as well.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    @Al Katawazi nope, in your PHP code. you are addressing a file in your PHP code. And you have to use RIGHT address – Your Common Sense Mar 19 '11 at 19:20
  • Sorry this wasn't it. I updated the code to look like this: – Al Katawazi Mar 19 '11 at 20:07
  • @Al Katawazi I've edited second part (using `__FILE__`), try it now. If fail, post it's error message. – Your Common Sense Mar 19 '11 at 20:17
  • [Sun Mar 20 01:53:43 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required '/home/viapics1/public_html/photo/common/configs/config.inc.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/viapics1/public_html/photo/index.php on line 2. That directory doesn't exsist. Its trying to go off photo and then to common when it should go to the html root directory and then go into common. I think the code was right and there is some other potential issue going on, perhaps in actually opening the file. – Al Katawazi Mar 19 '11 at 21:51
  • @Al Katawazi that's another error. you have to correct it similar way – Your Common Sense Mar 19 '11 at 21:58
  • require_once(dirname(__FILE__).'/ worked a treat for me – sapatos May 08 '14 at 10:38
  • dirname(__FILE__) is a good habit, even if don't need it for all cases. – m3nda May 26 '15 at 02:43
  • First in the answer you write it's not Apache related, but then the conclusion you give is to rely on the SAPI's DOCUMENT_ROOT parameter. While it's clear how this works in context of Apache (and the question), I find this a little contradicting for the introduction of the answer. And is there a reason why `__DIR__` (or that time perhaps `dirname(__FILE__)` is not mentioned? This could make it a good canonical answer. – hakre Jul 30 '23 at 11:14
11

If you have SELinux running, you might have to grant httpd permission to read from /home dir using:

 sudo setsebool httpd_read_user_content=1
user1533634
  • 431
  • 4
  • 5
5

You could fix it with the PHP constant __DIR__

require_once __DIR__ . '/common/configs/config_templates.inc.php';

It is the directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname __FILE__ . This directory name does not have a trailing slash unless it is the root directory. 1

Bernard
  • 607
  • 7
  • 11
4

Run php -f /common/configs/config_templates.inc.php to verify the validity of the PHP syntax in the file.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • The error message makes it clear that file doesn't exist, so what's the point of RERUNNING php just to confirm that it doesn't exist? OP has a leading slash, turning the path into an absolute one. It should be at minimum a relative path with no leading slash. – Marc B Mar 19 '11 at 19:43
  • 2
    "The error message makes it clear that file doesn't exist." No it doesn't. That identical error message could result from include path settings, file permissions settings, or safe mode settings. (I was also guessing that it could come from syntax errors, depending on the error reporting settings, but after some testing, it looks like PHP always shows the actual syntax error.) – Alex Howansky Mar 19 '11 at 19:58
  • You are wrong. Each of cases you mentioned has its own distinct error message. As well as syntax error. One could easily distinguish "parse error" from "file not found error". Your assumptions are all wrong and misleading. Get more experience with PHP – Your Common Sense Mar 19 '11 at 20:14
  • 1
    My error_log includes the stack trace, which contains the same text for these different conditions. E.g., here's a line pulled from my log just now (w/ paths obscured), for a non-existing file: ``PHP Fatal error: require(): Failed opening required 'sub/include.php' (include_path='.:/usr/local/lib/php') in /path/to/test.php on line 4'' And here's one for an existing file with bad perms: ``PHP Fatal error: require(): Failed opening required 'sub/include.php' (include_path='.:/usr/local/lib/php') in /path/to/test.php on line 4"'' Ditto for a bad include_path. – Alex Howansky Mar 19 '11 at 20:56
  • check file perms, dude. `failed to open stream: Permission denied ` is an error message for the bad perms – Your Common Sense Mar 19 '11 at 21:13
  • Read that more closely please. The text in the *stack trace* line is the same in each case (and precisely matches the wording posted by the OP), and the stack trace line follows the error line in the error_log. So if you're just watching the tail, you see the same message. In any case, it's not terribly relevant. If you don't like the answer, downvote it. You don't need to get all righteous and uppity. – Alex Howansky Mar 20 '11 at 03:25
2

Just in case this helps anybody else out there, I stumbled on an obscure case for this error triggering last night. Specifically, I was using the require_once method and specifying only a filename and no path, since the file being required was present in the same directory.

I started to get the 'Failed opening required file' error at one point. After tearing my hair out for a while, I finally noticed a PHP Warning message immediately above the fatal error output, indicating 'failed to open stream: Permission denied', but more importantly, informing me of the path to the file it was trying to open. I then twigged to the fact I had created a copy of the file (with ownership not accessible to Apache) elsewhere that happened to also be in the PHP 'include' search path, and ahead of the folder where I wanted it to be picked up. D'oh!

John Rix
  • 6,271
  • 5
  • 40
  • 46
0

you can set the include path in php.ini

include_path = ".:/home/viapics1/public_html"

Ahmed Abdelazim
  • 717
  • 7
  • 14
-7

I was having the exact same issue, I triple checked the include paths, I also checked that pear was installed and everything looked OK (but not using absolute paths) and I was still getting the errors, after a few hours of going crazy looking at this I realized that in my script had this:

include_once "../Mail.php";

instead of (it's behavioral equivalence):

include_once ("../Mail.php");

Yup, the parenthesis was not missing, but there was no generated error on this line of my script which was odd to me, and a I may have received this in stupid way.

hakre
  • 193,403
  • 52
  • 435
  • 836
Eduardo
  • 11
  • 7
    Because include_once is a language construct, and not a "real" function, the parenthesis are optional. Not sure why adding them fixed it for you. – Mike Hedman Feb 08 '13 at 18:07