Relative Path in Require_Once Doesn't Work

relative path in require_once doesn't work

Use

__DIR__

to get the current path of the script and this should fix your problem.

So:

require_once(__DIR__.'/../class/user.php');

This will prevent cases where you can run a PHP script from a different folder and therefore the relatives paths will not work.

Edit: slash problem fixed

PHP require_once Absolute Path vs Relative Path (Not Working)

../ denotes the parent directory, compared to the one you're currently in. Therefore, your first line says "go one directory up and look for config/constants.php in there". What you'd want to use with a relative path here is ./ (note the single dot), which denotes your current directory.

This has little to do with programming in specific, it's more of a file-system thing.

You can use this as a reference:

  • leading / (no dots, just a slash) means an absolute path
  • leading ./ (single dot) means the current directory
  • leading ../ (two dots) means one directory up
  • no leading dots or slashes is the same thing as with the single dot - relative to the current directory.

Update (to account for edited question):

What has something to do with programming and PHP in particular is that what is considered the "current" directory is usually the one where you initially executed the script from.
Under a CLI environment, that may vary, but if you're accessing the script via the web that is the location of the PHP file which you have directly accessed. E.g. if you're pointing your browser to example.com/test.php, then wherever 'test.php' is located is the directory you're currently in.

That can be changed by chdir() of course.

PHP require_once error with relative path

The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.

See the manual entry -> PHP Manual for __DIR__

__DIR__ will return C:\MyProject\phpunit-tesztek\include

You are looking to use the C:\MyProject\include\ path

__DIR__ . '/../../include/Form.php';

I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.

php : function require_once() path does not work

The current working directory is not guaranteed to be where the script is located. When you access index.php from the web server, your CWD is going to be ice_hockey, not ice_hockey/data.

The same would apply if you were running in the command line from the root directory and ran:

php ice_hockey/data/teams.php

The CWD would be the root directory, or where you executed the command from.

Use the magic constant __DIR__ to use the current script's directory.

require_once(__DIR__ . '/../view/top.php');

This will always point to the correct location relative to where teams.php is located.

My require_once has a right path but doesn't work

require_once '../../config/database.php';

Try this. I think you have to go backwards twice :)

what's wrong with my require_once path?

I have faced issues like this before, and the best way to handle this is to set your true filepath as a variable & prepend that to your includes/requires. Becuase the whole dirname(__FILE__) setup can act oddly in different environments especially those that use symbolic links. Explicitly stating where files are to be set is the best solution.

So let’s assume this is your codebase path; as per your example:

/home/zjkkvcxc/public_html/

Set that as a variable that all of your pages load in some way like this:

$BASE_PATH = '/home/zjkkvcxc/public_html/';

And now when you make calls to the file system for your app, do this:

require_once($BASE_PATH . 'php-sdk/facebook.php');

What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH to match your local environment. Like this might be a path for a MAMP (Mac OS X LAMP) setup:

$BASE_PATH = '/Application/MAMP/htdocs/';

Regarding how odd __FILE__ can act in symlinked environments, read up here:

Since PHP 4.0.2, _ FILE _ always contains an absolute path with
symlinks resolved whereas in older versions it contained relative path
under some circumstances.



Related Topics



Leave a reply



Submit