PHP Fatal Error: failed opening required

32,746

Solution 1

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');

Solution 2

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

Solution 3

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.

Share:
32,746

Related videos on Youtube

JM4
Author by

JM4

I studied engineering/management of technology but got tossed into the world of web development after working years in a project management capacity. Work with healthcare/insurance companies to develop custom database and reporting solutions along with online enrollment applications.

Updated on March 26, 2020

Comments

  • JM4
    JM4 about 4 years

    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.

  • JM4
    JM4 about 13 years
    I do not want to use a relative path - this will break several includes/requires
  • mario
    mario about 13 years
    @JM4. See edit. Use the second method. Else the include_path is your friend.
  • JM4
    JM4 about 13 years
    relative paths will not work for this situation. Provided new update. thanks for the help though.
  • JM4
    JM4 about 13 years
    @mario - i already set the include path as such within my htaccessfile. I do not want to hardcode the server document root in each call (also it will prevent me from ever testing in a windows environment
  • JM4
    JM4 about 13 years
    see other comments - relative paths do not work when additional sub includes/requires are made
  • mario
    mario about 13 years
    If you already have the include_path set, and the parent directory of includes/ is within, then include("includes/sess-start.php") should very much do the trick.
  • Mark Baker
    Mark Baker about 13 years
    @JM4 - Then use include('includes/sess-start.php'); so that PHP will use the include_path
  • JM4
    JM4 about 13 years
    @mario and @mark baker - regardless of the fact, the above should theoretically work. Removing a preceding slash is 'tricking' the system. In a windows environment the forward slash works (and is what Dreamweaver will place before ANY file which is relative to its site root)
  • JM4
    JM4 about 13 years
    Also - whether i set the include_path to the root or not, ('includes/xxx') would work because it is a relative path
  • Matthew
    Matthew about 13 years
    There's no reason for relative paths to not work. Set your include_path up with some base dir, and make everything work relative to that. If you insist on absolute paths, see the __FILE__ constant and grab the dir name from it.
  • mario
    mario about 13 years
    No, it should not. / always indicates an absolute path, not a virtual directory base. Dreamweaver has mislead you.
  • mario
    mario about 13 years
    @JM4: If you don't want to hardwire the include_path, then set it in an initialization script. set_include_path(..+..) instead of htaccess
  • JM4
    JM4 about 13 years
    please see (stackoverflow.com/questions/5434450/…). Relative paths WILL NOT work in the example provided in that question.
  • JM4
    JM4 about 13 years
    @Andrew Moore - thanks but I am also trying to avoid having to hardcode this on 184 different pages all which use include or require variables
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: Do a mass replace. My solution doesn't require hard-coding at all. The fact is, you'll have to modify your paths anyway. / designates an absolute path under Linux (and also should under Windows). Also, include_path doesn't cause problems under Windows.
  • JM4
    JM4 about 13 years
    @mario - in my last statement, I meant that removing the forward slash does not have anything to do with the include_path being set. ('includes/xxxx'); would work regardless because that particular example IS a relative path, regardless of what you set you include_path to (it is coincidence they match)
  • Matthew
    Matthew about 13 years
    @JM4, I would never suggest using '../' to begin an include path. I would make them relative to your include location, regardless of where the source file is.
  • JM4
    JM4 about 13 years
    @Andrew - thanks for the help. I'll have to keep thinking about it but the forward slash does not cause any issues within a windows xampp environment. Dreamweaver sets the forward slash automatically when using a site root-relative path (perhaps flawed in their code).
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: It is flawed... Just do realpath('/') and you'll figure it out pretty fast. Regardless of the environment, if a path starts with a slash /, it is always absolute. So, under Windows, /includes/sess-start.php points to C:\includes\sess-start.php. Under *nix, it points to /includes/sess-start.php. Don't believe me? realpath() it. All three answers says the exact same thing: MAKE YOUR PATHS ABSOLUTE. Maybe we are not wrong... Do a mass replace.
  • JM4
    JM4 about 13 years
    thanks. In most cases, relative paths are not an issue but in those which use sub requires it causes major headache. An example of such is a subdirectory using a connection class then within that connection class calling another require which contains the constants for that connection
  • mario
    mario about 13 years
    @JM4: Read the manual again, php.net/manual/en/ini.core.php#ini.include-path Everything else has been answered.
  • JM4
    JM4 about 13 years
    @Andrew Moore - ('/includes/sess-start.php'); under windows does not point to C:\includes\sess-start.php - the file works fine and i can tell you that directory under C does not exist. Again, I am trying to use absolute paths here, the issue being a replace and that the include_path theoretically should take care of the issue on its own.
  • JM4
    JM4 about 13 years
    @mario - I am not trying to be argumentative. My only point being that removing the preceding slash, in this particular example and the one we are discussing in this comment thread is that regardless of the include_path in this situation it is the same as the relative path so the issue is mute. I am trying to accomodate and assist many people. using the absolute path as above prevents dreamweaver from hotlinking the file for quick access when coding, something our team values very much.
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: Do a realpath('/includes/sess-start.php'). You'll get C:\includes\sess-start.php. If it works, then your PHP installation is bugged. RFC3986 clearly states that any and all paths beginning with a forward slash is absolute to the root of the current file hierarchy. No if, and, else or buts about it. (Yes, a path is a URI).
  • JM4
    JM4 about 13 years
    @Andrew Moore - I put a note below to mario but I'm not trying to be argumentative here, simply trying to find a blend of 'correctness' so all parties and programs can cooperate. Adding an absolute path as suggested above will work (yes I have tested this) as well as setting proper include_path; it however disables any hotlinking that can be done in dreamweaver to quickly and easily move between files and their requirements. Perhaps if I gave another example above it might clear up the issue we face.
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: What's more important, an IDE feature or a working script? You have to set your priorities straight here. We told you what's wrong with your approach and gave you solutions. If you refuse all possible solutions, we can't help you.
  • mario
    mario about 13 years
    @JM4: Yeah I got that. In this specific instance the include_path is not significant. But for any other relative includes, it can be. My point is that you simply should add all potential subdirectories where you might need it. -- But when your issue is actually Dreamweaver, then maybe you can trick it. Add some fake include statemens by enclosing them in a if (0) { block. Or even more sneaky: abuse the goto statement to have PHP skip over the fake for-Dreamweaver include("/...") files. (But maybe you just miss an option in DW to set the relative->absolute directory mapping.)
  • JM4
    JM4 about 13 years
    @Andrew Moore - fair point. Again, not trying to be combative, merely pointing out all the issues faced and see if there are additional solutions (take for example the relative path suggestion, valid and yet does not apply). Had I given the full update in my initial question properly it might have been more clear.
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: There are two types of paths... Absolute or relative. You rejected both. We don't do magic.
  • JM4
    JM4 about 13 years
    @Andrew Moore - yet again, my point being that the documentation much of this argument is based on is therefore flawed help.adobe.com/en_US/dreamweaver/cs/using/…, their own site gives 3 sets of relative paths.
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: That's for hyperlinks, not for PHP includes (which is why it's in the "About linking and navigation" section). In an hyperlink context, the root of your file hierarchy is the root of your web server. In PHP's context, which is the physical filesystem context, the root is your filesystems' root.
  • Andrew Moore
    Andrew Moore about 13 years
    @JM4: From PHP's Documentation, I quote: "If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Windows/Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file."
  • Andrew
    Andrew over 8 years
    It may be worth noting, if you get this error, your path may actually just be wrong.

Related