0

This works: require_once '../../include/common.php'
But this doesn't: require_once '/include/common.php'
But this works: <a href = '/include/common.php'>

I get this error:
Warning: require_once(/include/common.php): failed to open stream: No such file or directory in C://C:\wamp64\www\src\views\search.php

If it works as a link, why doesn't it work for require_once?

Thanks for your help :)

Hannah
  • 1
  • 1

3 Answers3

0

Using relative paths in require_once is generally considered as bad practice leading to error-prone situations.

The better solution is to set include_path in your php.ini file correctly.

Or you can use something like this:

require_once dirname(__FILE__) . "/../../yourscript.php";

Hope it helps.

Maertien
  • 11
  • 1
  • You can also use `require_once(__DIR__."/../../yourscript.php");` for more recent versions of PHP. – Adam J Apr 18 '18 at 14:26
0

If you have problems with require_once, always use absolute path (file system), like this:

require_once '/folder1/folder2/folder3/file.php'

In your case require_once '/include/common.php' is not "relative to the root". It's an absolute file system path that doesn't exist.

Try and print_r(__DIR__); in the folder were you want to include a file and see the absolute path to the file system.

The following <a href = '/include/common.php'> works because it's relative to domain name.

Reading this might help you understand the differences between file system path and web server path.

Binar Web
  • 867
  • 1
  • 11
  • 26
0
require_once '/include/common.php'

and

<a href = '/include/common.php'>

they not pointed on the same file. In first case, you will include common.php in /include at the root of the system file. In second case, you will include common.php in /include at the root of the VHost.

That's why ../../include/common.php are different of /include/common.php

Vincent Faliès
  • 298
  • 3
  • 10