9

What would be the reasons of a non working include_once?

Here's my folder hierarchy:

/Php/Controls/GridView/GridView.php
/Php/Controls/Form/Form.php

In "Form.php":

include_once '../GridView/GridView.php';

I'm getting this error:

Warning: include_once(../GridView/GridView.php) [function.include-once]: failed to open stream: No such file or directory in ...Form.php on line 4

Warning: include_once() [function.include]: Failed opening '../GridView/GridView.php' for inclusion (include_path='.;C:\php\pear') in ...Form.php on line 4

Please tell me if you want more information.

3
  • Add a echo getcwd(); before the include. The current path doesn't have to be the location of the file, that's a typical source for this kind of errors. Commented Apr 7, 2011 at 23:06
  • 1
    The include path is only used, if its a "real" relative path (does not start with / or .) , but with . or .. it always creates the pathes from the current workdir (see @svens comment) Commented Apr 7, 2011 at 23:09
  • possible duplicate of php include_once failing despite existence of file and 777 permissions Commented Aug 12, 2015 at 13:03

4 Answers 4

19

In Form.php use

include_once dirname(__FILE__) . '/../GridView/GridView.php';

This creates an absolute path, but relative to the file, where its called from.

Sign up to request clarification or add additional context in comments.

Comments

2

It can't find the file.

You should use a full path, like /Php/Controls/GridView/GridView.php instead of a relative one.

1 Comment

A problem with an absolute path is that when one decides to move/copy the website, many things will get broken...
1

Try this:

include_once './php/Controls/GridView/GridView.php';

Comments

1

Hope I'm not too late on this one but I thought this might be what you're looking for:

include_once realpath(dirname(__FILE__)."/../GridView/GridView.php");

Using realpath() allows you to include files even from outside your server document root directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.