I am getting the following error from Apache:

PHP Fatal error: require_once(): 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 definitely not an expert of Apache but the file config.inc.php and 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.

Any ideas?

Your Common Sense's user avatar

asked Mar 19, 2011 at 19:06

Al Katawazi's user avatar

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.

answered Mar 19, 2011 at 19:12

Your Common Sense's user avatar

8 Comments

@Al Katawazi nope, in your PHP code. you are addressing a file in your PHP code. And you have to use RIGHT address

2011-03-19T19:20:27.297Z+00:00

Sorry this wasn't it. I updated the code to look like this: <?php require_once($_SERVER['DOCUMENT_ROOT'].'/common/configs/config.inc.php'); and I am still getting the exact same error. Interestingly when I put an X in the url it goes back spells out the entire URL like this: Failed opening required '/home/viapics1/public_htmlX/common/configs/config.inc.php', its all pretty strange, could there be somekind of global override going on? The really strange part is this app was working on another server but when moved to the new one I started having this issue. Thanks so much for the help so far.

2011-03-19T20:07:12.947Z+00:00

@Al Katawazi I've edited second part (using __FILE__), try it now. If fail, post it's error message.

2011-03-19T20:17:38.097Z+00:00

[Sun Mar 20 01:53:43 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: 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.

2011-03-19T21:51:27.853Z+00:00

@Al Katawazi that's another error. you have to correct it similar way

2011-03-19T21:58:46.847Z+00:00

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

answered Dec 23, 2014 at 16:54

user1533634's user avatar

2 Comments

do i need to run this command from /home dir?

2022-04-27T17:34:08.007Z+00:00

you should not need to as /home should already be labelled as user content.

2022-04-29T02:08:04.097Z+00:00

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

answered Feb 27, 2019 at 4:35

Bernard's user avatar

1 Comment

For the OP, it won't work. Because it will result in /home/viapics1/public_html/common/configs/common/configs/config_templates.inc.php. The __DIR__ constant is not a magic wand that will solve every single problem with filenames. Like any tool, it must be used with understanding

2023-11-15T11:22:56.663Z+00:00

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

answered Mar 19, 2011 at 19:09

Alex Howansky's user avatar

6 Comments

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.

2011-03-19T19:43:48.91Z+00:00

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

2011-03-19T19:58:55.473Z+00:00

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

2011-03-19T20:14:25.51Z+00:00

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.

2011-03-19T20:56:37.727Z+00:00

check file perms, dude. failed to open stream: Permission denied is an error message for the bad perms

2011-03-19T21:13:15.763Z+00:00

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!

answered Dec 3, 2017 at 23:44

John Rix's user avatar

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's user avatar

hakre

200k55 gold badges455 silver badges870 bronze badges

answered Dec 23, 2012 at 0:45

Eduardo's user avatar

1 Comment

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.

2013-02-08T18:07:14.53Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.