Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Fatal Error: failed opening required

I have a script called 'sess-start.php' which lies in an /include directory within my httpdocs directory.

My site continues to give this error:\

[Fri Mar 25 14:52:24 2011] [error] [client 12.3.3.3] PHP Fatal error: require() [function.require]: Failed opening required '/includes/sess-start.php' (include_path='.:') in /var/www/vhosts/site.com/httpdocs/page.php on line 4, referer: https://www.site.com/

even though www.site.com/includes DOES exist. What gives!

Edit 1

These are includes/requires which may themselves contain additional require or include statements. Relative paths WILL NOT WORK so please do not suggest such.

My .htaccess file already points all include paths to the site root directory:

php_value include_path .:/var/www/vhosts/site.com/httpdocs

Edit 2

Not all of my include or required scripts are contained with a single directory so suggesting to simply place the /includes directory in the include_path is negated as well (and in turn also causes problems on a windows machine)

Update

Perhaps some clarification on a real-life example may help to resolve the issue our team is trying to solve for:

On one page, a user may enter a number of options, the following page makes its necessary calculations and based on that will route the customer in a number of potential options, all which lead to require statements for something like a DB entry.

Then, within the db entry (or some other action) if everything goes smoothly, the member may have chosen before to receive an email confirmation based on his/her action. This require statement lies within the 2nd require (db insert) but is in a different directory than the second and thus causing conflict given the first file treats the linking incorrectly.

Hard coding the absolute path or even setting the appropriate include path per page is 'ok' but then it disables our team's ability to hotlink between files with dreamweaver (or any other program that does the same) because it does not recognize a 'site root' when running in a test environment.

like image 471
JM4 Avatar asked Mar 25 '11 19:03

JM4


3 Answers

Your path is preceded by a forward slash (/).

In a POSIX compliant system, if you have a path that starts with a forward slash, it means that it is an absolute path. The first forward slash represents the root of the filesystem.

Remove the forward slash from your path and you should then have a path that is relative to page.php.

EDIT: Since relative paths won't work, you can use dirname(__FILE__) to get the absolute path of the directory where the current file resides.

require(dirname(__FILE__) . '/includes/sess-start.php');
like image 193
Andrew Moore Avatar answered Nov 12 '22 11:11

Andrew Moore


You need to use a relative path. When your filenames start with / PHP will assume you mean the root directory. The correct prefix is ./

include('./includes/sess-start.php');

If you want all your paths to be relative to the document root, then this is a common method:

include("$_SERVER[DOCUMENT_ROOT]/includes/sess-start.php");
like image 6
mario Avatar answered Nov 12 '22 11:11

mario


Looks like you need to remove the leading slash from the include line.

Edit: To address your concerns. In a prepend file or some common include, create a constant like DOCROOT. You can dynamically determine it from your __FILE__ constant.

Then:

include(DOCROOT.'myfile.php');

Personally, I would try to set things up to avoid this sort of thing.

like image 1
Matthew Avatar answered Nov 12 '22 11:11

Matthew