Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use absolute or relative path?

Tags:

php

in my config.php where i have all constants i set the PATH to a absolute path.

but this means that when i move my application folder i have to change this path.

i wondered if its better to set a relative path, in that way whenever i move my application between production and development folder, i dont have to change it.

how do you guys do when you move between folders?

like image 922
never_had_a_name Avatar asked Apr 09 '10 01:04

never_had_a_name


People also ask

Should I use absolute or relative path?

When a user clicks a relative link, the browser takes them to that location on the current site. For that reason, you can only use relative links when linking to pages or files within your site, and you must use absolute links if you're linking to a location on another website.

Should you use relative paths?

Relative paths rely on the current working directory. This is a form of global state, and as such it should be avoided whenever possible. Depending on the structure of your application, the current working directory may not be guaranteed to be constant through all code paths that can reach a certain routine.

Why is the relative path preferred over the absolute path?

Creating an absolute path for connecting different websites Whether the link opens in a tab or a window depends on the user's browser settings. On the other hand, relative links connecting pages within your own website should always load in the same browser page.

Why is it a best practice to use relative path?

Using relative paths is a good coding practice regardless of the language or program in which you are coding. It allows for portability of the code between directory locations on a single machine, or if copied or accessed on multiple machines.


3 Answers

The best way I've found is to do the following:

define("PATH", realpath(dirname(__FILE__)));

That gives you the directory of the current file. If you do this in your settings/bootstrap/init file, you'll have it available to your application, and it will work for any file system.

like image 109
zombat Avatar answered Sep 22 '22 01:09

zombat


__FILE__ is your friend.

like image 20
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 01:09

Ignacio Vazquez-Abrams


define('BASE_PATH', dirname(realpath(__FILE__)));

This will make your scripts more portable.

Include a file like this

include BASE_PATH . 'includes/header.php';
like image 44
alex Avatar answered Sep 24 '22 01:09

alex