Relative Path versus Absolute Path

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Relative Path versus Absolute Path

Post by devendra-m »

Is there any advantage of using absolute path over relative path

like

Code: Select all

require('http://www.domain.com/test/test.php');
and

Code: Select all

require('test/test.php');
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

FYI, your first example is not a full path, it is an URI. A full path would be something like

Code: Select all

<?php
require '/full/path/to/some/file.php';
?>
A relative path would be something like:

Code: Select all

<?php
require 'file.php';
?>
or

Code: Select all

<?php
require '../file.php';
?>
To answer your question, there is no real advantage to using either. When using a URI you have to have certain settings turned in php.ini in order to open files over HTTP
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Post by devendra-m »

require('http://www.domain.com/test/test.php');

but it also works. Is it wrong practice to use path like this
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I wouldn't do it that way. If it is on the server, call it from the server, not from the web.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

devendra-m wrote:require('http://www.domain.com/test/test.php');

but it also works. Is it wrong practice to use path like this
When you include a file that way, PHP will send an HTTP GET request to your server to retrieve the contents of the file, just like a browser would. That means that Apache and PHP will process the file and deliver the processed output to your "require" statement.

When you include a file from the filesystem, the entire, unprocessed contents are dumped directly to your script.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

An advantage to using relative paths to absolute paths???

Relative paths usually result in more portable code...you can move entire folders around without having to update a ton of paths in your source code.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: Relative Path versus Absolute Path

Post by webspider »

devendra-m wrote
require('http://www.domain.com/test/test.php');

but it also works. Is it wrong practice to use path like this
may arise an issue of code injection. turn off allow_url_fopen and allow_url_include in php.ini
Post Reply