This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Absolute path & Relative Path
This is a very basic question, but I have been scratching my head over this for a long time and I am hoping someone can help clear up my confusion.
I have a shared account on a linux server.
The path to public_html is: /home/myusername/public_html
The code for my website lives here: /home/myusername/public_html/mysite.com
Under mysite.com there are directories such as 'mycss', 'myjs', 'myphp', etc.
When I create an absolute path (for example, within php code or an html file), sometimes the path needs to start at /home, eg /home/myusername/public_html/mysite.com/myphp/myfile.php
And sometimes it needs to start inside mysite.com, eg /myphp/myfile.php
My confusion: When does an absolute path need to start at /home, and when does it need to start within mysite.com? Is there a rule of thumb, or some insight anyone can give?
You are confusing two types of absolute / relateive.
IF you are speaking in website terms (images / js / html stuff), your absolute is the path from your root website folder (ie: /home/myusername/public_html/mysite.com)
So if you have a folder like:
/home/myusername/public_html/mysite.com/images/bob.jpg
In your HTML, the absolute path would be /images/bob.jpg, while a relative website path would be images/bob.jpg etc;
IF you are doing PHP / Server side includes, you need to use the FULL absolute path, so your whole /home/myusername/public_html/mysite.com stucture.
But also at the same time, if you want to use a relative method (preferred in most cases as it makes your PHP code easier to move) you would just do an include('../database/connect.php');
It depends on the website. Did you write the PHP code for the website? Usually the absolute path will point to the web root (eg. /home/myusername/public_html/mysite.com ), but if you have config or other included files that are outside of the web root for security purposes - that might require a different path (eg. /home/myusername/notwebroot )
You're confusing what is actually a relative path with an absolute path.
/home/myusername/public_html
is an absolute path
/public_html/anything
/myphp/myfile.php
/mysite.com/anything
these are all relative paths.
If it isn't starting at the root then it isn't an absolute path. If it is starting at the root of your website then it is a relative path because if you move your website to a different folder then the paths to these directories and files don't need to change (i.e. they are relative to the web root).
Related
I recently was watching a php video tutorial and the author was showing how to do include a file. He was using XAMPP for the demonstrations and had many files.
When he was showing how to include a file, he mentioned something about putting two dots (..) in front of the file path (/xampp/content/example.html) because of something having to do with where the files were located, assuming that I already had knowledge of this principle. But i don't.
Can anyone explain what is up with having one dot or two dots in front of file paths?
What is the difference between include("/xampp/content/example.html");, include("./xampp/content/example.html");, and include("../xampp/content/example.html");
In Linux / Unix environment,
/xampp/content/example.html means absolute path
./xampp/content/example.html means relative path of current directory
../xampp/content/example.html means relative path of parent directory
For the folder structure: /var/www/xampp/content/example3.html:
If your current folder is /var/www/...
../ (goes up 1 level) will be /var/
./ (in current level) will be /var/www/
/ will be / (in Linux, / means the root of the server, the outermost structure of the filesystem)
../../ (goes up 2 level) will be /
There are 2 types of paths: Relative Path & Absolute Path.
For Relative path, it's relative to your current directory. For absolute path, it's not related to your current directory.
. means the same directory as the script that's doing the including, .. means the parent directory of the one containing the script. So ../xampp/content/example.html means to go up one folder level from the current script, then go into its xampp/content subdirectory to find example.html.
A path beginning with / is an absolute path from the root of the server. Using absolute paths makes it harder to move your project to a new directory, because you'll need to update all the paths. Relative paths allow you to move everything as a group without changing the paths, because the directory relationships will stay the same.
Consider the following scenario: I have a vhost defined to some paths on my home folder.
say ~/web/project-name/ is my root. such that when i point to http://some-name/ it points to the index.php inside ~/web/project-name.
I've a Model-View-Controller framework (self-made/minimal) and my views contains different client side links (js, or css, or a href) Since I made my working folder root, i used absoulte path names (for instance /client/css/my.css ).
Now a friends comes in takes my projects. Copies it to /var/www/ So, now the contents of my website is not root, so my links in the views does not work?
What is the best way to mitigate the above problem?
I tried defining a constant ROOT as define('ROOT', dirname(__FILE__)) in my index.php, but it returns the absolute path like /home/cipher/...
I want to make a function such that it returns the path of my index.php relative to the web root!
Thanks in advance!
You might want to try a $_SERVER['DOCUMENT_ROOT'].
I have a web application in php. I have a header.php file where I have the opening html tag, meta data, css,js file includes.
I have multiple folders in this application. I have given relative paths to the css,js files in the header.php. The problem I am facing is, when I include this header.php file in some other file say, ./test-folder/my-file.php, the relative paths break.
so to solve this i have given absolute paths where ever needed. But I have to change these paths every time I upload on to server. Can this be done in any other way?
Thanks in advance
Anji
Do not use static value for path instead define a constant for document root and use that constant
define('ROOT_PATH',$_SERVER['DOCUMENT_ROOT']);
While including files you can use this constant.
include ROOT_PATH.'/some_dir/file.php';
You have bad architecture in your application. Try to search info about MVC.
This answer may be useful: https://stackoverflow.com/questions/5721353/what-is-good-neat-architecture-in-programming/5793753#5793753
So:
change architicture,
use autoload
Relative paths are relative to the current working directory (cwd). If you just include another file that cwd does not change.
If you want to have paths that are (somewhat) relative to the current script file path you can use e.g.
require dirname(__FILE__).'/some_dir/file.php';
or as of php 5.3
require __DIR__.'/some_dir/file.php';
see also: http://docs.php.net/language.constants.predefined
If I understood your question right, you just would like to know the root folder your application without the need to hardcode it.
The easier way is to define a constant as Shakti has answered.
However, you could actually have a file with a unique name such as "foobar" in the root folder, then walk up the tree from current directory until you find foobar.
I am building a web app (a forum), and it is to be integrated into various websites. The idea is that the folder location storing all the files is variable so that a webmin can put the forum wherever they like. I have, as usual, some PHP included files.
Say I have global.php and envars.php and want them included in app_root.php. The first two are stored in ./global/, relative to app_root.php. Now, when I use ./ I get a file not found error. If I use just global/ (no prepended slash), I get the same error.
I really need help on this :-(
The paths need to be relative to app_root.php and can't be absolute - the abs. path varies per installation.
Thanks for reading,
James
This problem is normally solved by creating a constant with an absolute path to the application.
Something like this in your app_root.php file
define('ROOT_PATH', dirname(__FILE__));
Then, to include other files just use something like
include ROOT_PATH . '/dir/file.php';
I'm having trouble specifically with the getimagesize function. I'm making the function call from /item/ajax/image.php relative to the domain's HTTP root. I'm trying to get the dimensions of an image stored at /portfolio/15/image.jpg. From what I understand, the function takes a filename as an argument, so I tried the following:
getimagesize('/portfolio/15/image.jpg')
And
getimagesize('../../portfolio/15/image.jpg')
But both of them just threw PHP errors.
try prefixing below to path:
$_SERVER['DOCUMENT_ROOT']
Relative paths always start from the file that is executed, which is most likely index.php. This is true for included files as well. This means in any file within you project relative paths start from your index.php. (Except a chdir() is done before)
I think it is really bad code to have paths like "../../file.ext" or the like. Define a Constant that has the full path to your application (eg: $_SERVER['DOCUMENT_ROOT']) and prepend it to any path you're using.
Example:
# somewhere in your index.php
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
# in any included file
$my_path = ROOT_PATH."/portfolio/14/image.jpg"
This is imho the cleanest and most readable way to define paths.
In PHP "/" is not the same as the Apache "/" (web root). In PHP "/" refers to the system root. You should use paths relative to your PHP script location ('portfolio/15/image.jpg' if your script and the 'portfolio' folder are in the same location)
The filename you enter is not related to the http root but should be an existing path in the file system of your web server.
To see what goes wrong you could enter:
realpath('../../portfolio/15/image.jpg')
To see what directory you end up at.
Or use:
imagesize(dirname(__FILE__) . '/../../portfolio/15/image.jpg')
to get the full directory qualification.
As an alternative you can use the web address, but you should specify the full url:
getimagesize('http://yoursite.com/portfolio/15/image.jpg')
However, this is a slower option.