1

I'm trying to require some files but it keeps telling me that they can't be found. The files are located in separate folders.

index.php -> ../core/init.php -> ../library/autoload.php

http://pastebin.com/rG3bvSzn

Could anyone help me figure this out? It's been driving me insane for the past hour.

Puck
  • 2,080
  • 4
  • 19
  • 30

3 Answers3

1

If you're trying to include or require files, the path needs to be from the full root. Try this:

<?php
$root = $_SERVER['DOCUMENT_ROOT'];
// This is where index.php probably is

require_once($root.'sanitize.php');
require_once($root.'general.php');
?>

If you echo out $root you'll see the actual path of your files.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Thanks for your help, I tried that and it brings me to the absolute root of the server, I have multiple projects. This was all working before I moved the files around into seperate folders. – Patrickbizzle Jul 11 '15 at 22:14
  • Well, you need to locate what folders your files are in from the root, and add that as `$root.'/folder/libaries/general.php'`. If you use ? `$root.'general.php'`, the require will assume that the files you are including are in your root folder. You need to add the folders in between. – Qirel Jul 11 '15 at 22:16
  • I fixed it by moving the file calling it (init.php) theres no possible security issue with init being the root directory is there? Im trying to build an MVC application. – Patrickbizzle Jul 11 '15 at 22:18
0

No, you don't need full path for require scripts.

The problem is that in your autoload.php it's located in ../library/ folder and when you try an "require_once" the path starts in ../library/

The "require" start in the folder of the main script that calling the function.

/somefolder/index.php

require('../core/init.php');

/core/init.php

require('../library/autoload.php');

/library/autoload.php

print "hello";
Nomad Webcode
  • 816
  • 5
  • 9
0

And make your path relatively.

For PHP < 5.3.0 try,

$rootpath=dirname(__FILE__);

require_once($rootpath.'sanitize.php');
require_once($rootpath.'general.php');