Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute vs. relative paths

If I use absolute paths, I can't move the whole directory to a new location. If I use relative paths, I can't move individual files to new locations.

What's the solution here? Do you set up a config file that holds the root path and go from there? Or do you have a rule like: Never move files around?

I've seen in some projects that people use dirname(FILE). What is the point of that, I mean, why not simply leave it out since the dirname is relative anyway (depending on where the file sits)?

like image 339
Frank Vilea Avatar asked Dec 02 '11 12:12

Frank Vilea


People also ask

What is the difference between absolute and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

Should I use relative or absolute paths?

If you do a lot of testing and move content frequently between domains, then relative links are the best solution. Absolute paths are more secure, protect content, and prevent duplicate pages. The main point is that the link format you prefer should be applied to all URLs on the site.

What is absolute path and relative path Example?

For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string. A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path.

What is absolute path and relative path in Windows?

If all three components are present, the path is absolute. If no volume or drive letter is specified and the directory name begins with the directory separator character, the path is relative from the root of the current drive. Otherwise, the path is relative to the current directory.


1 Answers

you should use a config file that will be included in each file first line, for example your app look like this

root / App / Plugins

inside your root dir : app-config.php

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

now, suppose you have to include a plugin file, so

inside your Plugin dir : my-plugin.php

require_once '../../app-config.php';

now everything below this line can use ABSPATH

example do you want to load an image

<img src='".ABSPATH."Public/images/demo.png' alt=''/>

now, the thing is more simple if your app is designed to automatically load some files like

plugin-widget-1.php

so that everything inside this file or any other file loaded by the my-plugin.php file can use the ABSPATH without include each time the app-config.php file.

with this in mind you can have all the short-hand you want into the app-config.php example

define('UPLOAD_PATH', ABSPATH. 'Public/uploads/');
define('IMAGES_PATH', ABSPATH. 'Public/images/');
define('HELPERS_PATH', ABSPATH. 'App/helpers/');
...

so, now that you have all defined, if you need to move a file, let's say one folder forward example:

root / App / Plugins / Utils

just inlucde require_once '../../../app-config.php';

obviously i suppose that you are not changing paths each time =) anyway if you need to do so is always more simple to change one file inclusion instead of hundreds.

hope this make sense to you =)

like image 145
Luca Filosofi Avatar answered Sep 19 '22 03:09

Luca Filosofi