0

Firs of im pretty new in PHP.

I want to include a php file from root in another php file like this.

require_once "/getPublisher.php?id=' . $xy;

The file where i type this code is in '/page/page.php'.

--------------- ROOT -----------------
    -page
       -> page.php----------------
                                 - trying to require_once '/getPublisher.php'
                                 - into '/page/page.php'
    -getPublisher.php-------------

I am getting these error codes back.

Warning: require_once(/users/uplade/www/getPublisher.php?id=123) [function.require-once]: failed to open stream: No such file or directory in /users/uplade/www/page/page.php on line 83

and

Fatal error: require_once() [function.require]: Failed opening required '/users/uplade/www/getPublisher.php?id=123' (include_path='.') in /users/uplade/www/page/page.php on line 83

So how to include root files in non-root files?

EDIT:

Warning: require_once(/getPublisher.php) [function.require-once]: failed to open stream: No such file or directory in /users/uplade/www/page/page.php on line 84

WHAT IM DOING WRONG.

SOLUTION: I removed the slash require_once 'getPublisher.php'; Thats way it worked for me!

  • Is your file located at /users/uplade/www/getPublisher.php? – Webomatik May 05 '16 at 13:26
  • include/require files by filesystem path, not web path; and your'e including a file by name, nut as a url, so don't pass args in that way – Mark Baker May 05 '16 at 13:26
  • @Webomatik yes it is. Removed all params, still gives same error as before. –  May 05 '16 at 13:30
  • You're including a file in `/users/uplade/www` from a file in `/users/uplade/www/page`, so perhaps `require_once "../getPublisher.php";` rather than using an absolute filesystem reference from the filesystem root – Mark Baker May 05 '16 at 13:37
  • 1
    Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew May 05 '16 at 19:05

3 Answers3

0

You can't use parameters on require(). Try removing it:

require_once "/getPublisher.php";
Webomatik
  • 844
  • 7
  • 7
0

With require_once "/getPublisher.php"; you are trying to include a file from the filesystem root, not from the document root.

If the files would be in the same directory, you would include it like this: require_once "getPublisher.php";

But in your case, it's one level up, so just include it like this: require_once "../getPublisher.php";

cili
  • 317
  • 4
  • 15
0

reuire and include (and their _once variants) locate a file on the filesystem based on its absolute path, falling back to a configurable set of include paths (used for placing shared libraries in standard locations).

There are two main ways to reference a file reliably when you don't know exactly where on the file system it will be (e.g. because you're going to install the software into different locations on different servers):

  • Relative to the "document root", that is the path configured in the web server to be the location of the URL "/". This directory is available as the superglobal variable $_SERVER['DOCUMENT_ROOT']; e.g. require $_SERVER['DOCUMENT_ROOT'] . '/getPublisher.php';
  • Relative to the current file, that is the file you are writing the PHP code in (regardless of how that file was accessed). This directory is available as the magic constant __DIR__; e.g. require __DIR__ . '/../getPublisher.php';

Notice in the second example I had to "climb up" from the current directory using .. which means "parent directory"; you can climb as high as you like with this: '/../../../../' etc

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • There is a now a canonical for that topic here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew May 06 '16 at 19:56