0

Say we have a folder domainname.com/somelibrary/library.php

Located in domainname.com/helloworld/someplace/someplace/somephp.php

I want to include that /somelibrary/library.php

A way to do so is to find the relative path. I can do

include_once ('../../../somelibrary/library.php');

But then the include_once statement must change all the time depending on where my file is.

I can try include_once ('/somelibrary/library.php');

It doesn't seem to work.

Warning: include_once(/blablabla/hello.php): failed to open stream: No such file or directory in /home1/romancegua/public_html/russia/test.php on line 2

I am very sure /blablabla/hello.php exist.

It's located at /home1/romancegua/public_html/blablabla/hello.php

So what's my solution?

2
  • 2
    I mean thats why name space and auto-load are developed .. Commented Dec 29, 2016 at 14:17
  • I suggest you get a look at how Composer and its autoloading works, it allows you to avoid this kind of relative path fiddling. Commented Dec 29, 2016 at 14:18

4 Answers 4

2

You have to use the absolute path of "somelibrary/library.php"

include_once('/home1/romancegua/somelibrary/library.php');

If you not sure how to get you can put an

echo __FILE__; die();

into library.php and call it. then it will show you the current file path.

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

2 Comments

hmmm is there a way to figure out the absolute path of the / directory in the domain? It'll be pretty useful
if you do not have ssh-access you can do also with some new php file. Make new test.php file and put <?php echo FILE; die(); ?> inside and call it with your browser
1

One way is to define paths constants, one with the path of your site

define('SITE', '/home1/romancegua/public_html/');  

and the one with your library folder

define('LIBRARY', SITE . 'somelibrary/');  

here I've made two for reusability.
an then you can call your library with:

include_once(LIBRARY . 'library.php');

Comments

1

Like the guys said in the comments, you should be using autoloading:

PHP has this feature natively:

spl_autoload_register(function ($class_name) {
    include $class_name . EXTENSION;
});

$obj  = new MyClass1();
$obj2 = new MyClass2(); 

$obj->sayHello();

Even easier, you can use namespaces and the autoloading feature offered by composer, using psr-4.

Example:

Add the following to your composer.json:

"autoload":{
  "psr-4" : {
    "App\\": "src/"
  }
}

App would be name of your project, and src is the folder for your classes.

Then use namespace

namespace App\folder;

http://php.net/manual/en/function.spl-autoload-register.php

https://getcomposer.org/doc/01-basic-usage.md#autoloading

3 Comments

is this what wordpress use?
yes, as you can see it here github.com/WordPress/WordPress/…
include is fine for small scripts, but then its not maintainable for projects
0
include_once ( $_SERVER['DOCUMENT_ROOT']..'/blablabla/hello.php');

That does the trick for me.

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.