1

I'm using simple include files for common elements on a mostly static website. I do so like this

<?php include('/inc/header.php'); ?>

But this doesn't work unless i remove the leading slash, and then it only works on pages in the root directory. What am I doing wrong?

Ben
  • 2,917
  • 10
  • 28
  • 47
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:52
  • 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 Sep 10 '16 at 09:25

5 Answers5

3

/... is an absolute path on unix systems. To specify a relative path use ./.... That will be relative to the called file's directory.

NikiC
  • 100,734
  • 37
  • 191
  • 225
3

'/' means the real server root directory.

If you need web document root use :

 include("{$_SERVER['DOCUMENT_ROOT']}/inc/header.php");
TECNO
  • 162
  • 2
  • 3
  • 15
mario
  • 144,265
  • 20
  • 237
  • 291
  • 1
    I went a step further and did this: set_include_path("{$_SERVER['DOCUMENT_ROOT']}/inc"); so that I could call all the includes using just the file name like this: include('filename.php'); – Ben Oct 29 '10 at 16:30
  • 2
    For portability sake, I wouldn't recommend using any of the `$_SERVER` paths... I recommend [`dirname(__FILE__);`](http://stackoverflow.com/questions/2893088/best-method-for-creating-absolute-path-in-php-see-3-methods-listed-inside/2893104#2893104) since it will always work 100% of the time... – ircmaxell Oct 29 '10 at 16:31
  • There is now a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:53
  • @VicSeedoubleyew Nice reference. Now if you want to make it a universal one, then mentioning common patterns like utilizing `DOCUMENT_ROOT` or a custom app root constant would make sense. – mario Apr 12 '16 at 23:29
  • @mario thanks for the suggestion. At which point in the answer do you suggest I add it ? – Vic Seedoubleyew Apr 13 '16 at 13:29
0

Use this solution

I also facing same problem Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory

Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')

But I solve them after searching it on google in 2 dyas.

Solution: you have to define your path then use include function as blow:

define (DOC_ROOT_PATH, $_SERVER['DOCUMENT_ROOT'].'/');

you can use any directory structure after that.

Ramiz Syed
  • 51
  • 1
  • 1
0

You have an absolute path - i.e. starts with /. So it is looking in your server root.

Without the slash it is a relative path and will look relative to the PHP file's path.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

add "."before path like that :

<?php include('./inc/header.php'); ?>

The path meanings:

  • / is the root of the current drive.
  • ./ is the current directory (that what you want).
  • ../ is the parent of the current directory.
TECNO
  • 162
  • 2
  • 3
  • 15