user1391152
user1391152

Reputation: 1289

PHP __DIR__ of file and backup by one directory

I am running a laravel server using PHP 7. I am trying to require a file using the following:

require __DIR__.'/../bootstrap/autoload.php';

The file I am running this script in is located in a folder called public. So therefore __DIR__ should equal:

www.mydomain.com/myserver/public/index.php

The path the script should create should be:

www.mydomain.com/myserver/bootstrap/autoload.php

However, rather than dynamically create the correct path it is treating the "/../" as a literal directory instead of telling it to go back one. So the path I am getting back is:

www.mydomain.com/myserver/public/../bootstrap/autoload.php

Any ides why this could be happening? Many thanks in advance.

Upvotes: 4

Views: 7497

Answers (3)

IMSoP
IMSoP

Reputation: 97898

Firstly, __DIR__ refers to a directory on disk, not a URL, and not a file. So this statement is wrong:

The file I am running this script in is located in a folder called public. So therefore __DIR__ should equal: www.mydomain.com/myserver/public/index.php

__DIR__ will be something like '/var/www/mydomain-com/public' on a Linux system, or something like 'C:\wamp\mydomain-com\public' on a Windows system.

The code you have will therefore generate a string like '/var/www/mydomain-com/public/../bootstrap/autoload.php'. This path is equivalent, at least on Linux and similar systems, to /var/www/mydomain-com/public/autoload.php so your code should work fine and is the normal way of writing this.

If you actually want to generate the string '/var/www/mydomain-com/public/autoload.php', you can use dirname(__DIR__) as indicated in other answers. That removes the last section from a path, so gives you (using my imaginary path above) '/var/www/mydomain-com'.

Upvotes: 1

apokryfos
apokryfos

Reputation: 40681

From the manual for realpath:

realpath() expands all symbolic links and resolves references to /./, /../ and extra / characters in the input path and returns the canonicalized absolute pathname

Therefore the following should work

require realpath(__DIR__."../bootstrap/autoload.php")

However this should not be necessary since require can resolve relative paths on its own just fine.

Since you are using laravel the recommended way is to do:

require base_path("boostrap/autoload.php")

However the actual recommended way is to sit back and wonder why you need to do this to begin with since if you boot laravel properly it should do all these things for you and you should never need to do require on anything.

Upvotes: 4

anon
anon

Reputation:

As per my comment;

__DIR__ is the current directory you are in, if you wanted to go back one step, you could use dirname

This is used such as;

dirname(__DIR__);

So, if your current directory is (and is where you actively are):

C:/server/htdocs/system/main/index.php

__DIR__ will give you C:/server/htdocs/system/main and dirname(__DIR__) will give C:/server/htdocs/system/

Therefore, using the following will do what you need;

require(dirname(__DIR__) . "/bootstrap/autoload.php");

Upvotes: 6

Related Questions