Including a PHP file, treats relative URLs from that directory - php

I have a PHP file at my server root.. index.php .. which include's .. DIR/main.php
Now .. DIR/main.php .. include's many nearby PHP files using relative URLs using .. include("./common1.php");
Any way I can change the relative-URL base path so when including "DIR/main.php" It can relatively access its nearby PHP files like "DIR/common1.php", instead of trying to find "common1.php" at the site root.

Take a look at set_include_path
Edit: When appending paths to include_path be sure to use the PATH_SEPARATOR constant as it is intended to make your include path OS agnostic.
<?php
set_include_path(implode(PATH_SEPARATOR, array(
get_include_path(),
'/DIR1',
'/DIR2/DIR3',
dirname(__FILE__),
)));
?>

First, set the "relative-URL base path" to your directory
set_include_path( get_include_path() . '/DIR' );
Second, include your file!
require( 'main.php' );
That should work though I've not tested it.

In addition to lonut's answer, you can get the directory of the file using a combination of the __FILE__ constant and dirname().
$include_path = dirname(__FILE__);
set_include_path($include_path);

Related

Relative URLS across different sub folders

Looking for a way of allowing my links and include URLs etc to work on my local machine correctly as well as on my live site.
I have for example a common.php file which contains my DB connection.
I also have a init.php file which is included on every page and inside that includes the common.php file (among others)
For now, i have used
include './common.php';
However, if i am in a page e.g. web/settings
the ./ points to the settings folder.
What should i be using as a relative URL that will work across the whole site no matter what folder etc?
How about /? It refers to the base, and from there you can use the absolute path:
include "/absolute/path/to/file/common.php";
A relative URL is always affected by the current directory, and you can't make it the same no matter where you are on the site. You need to use absolute paths.
You could use $_SERVER['DOCUMENT_ROOT'] for this.
set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] );
// Now, you can specify your path relative to your DOCUMENT_ROOT.
include('common.php'); // Assuming your common.php file is in your root path.
You'll find it alot more convenient using namespaces though, so you might want to go down that road.
the quick answer for a path is this.
__DIR__ = current working directory so If you have MVC type architecture ( single point of entry aka front controller, basically everything starts off in one file, typically index.php, and the rest are included ) you can just define a constant like this in that main file.
define( 'BASE_PATH', __DIR__.'/' );
So if you have like this
root
index.php //define basepath
includes :
other.php
template :
temp.php
in other you can just do
include BASE_PATH . 'template/temp.php';
everything will be tied by that one base set in the main index.php file, and as long as the folder i put above as root contains everything you can move that where ever you want because of the dynamic part __DIR__
The long answer is to use a PSR-0 autoloader but that might be a bit overkill.
As a side not if you are on PHP < 5.3 use this instead of __DIR__
dirname(__FILE__)

PHP - Relative paths to included and required files

Well, i am working with a framework that is composed of a somewhat complex file and directory structure.
in my application, i just need to require a file, like so:
require "framework/mainfile.php";
inside mainfile, it is including other files, like:
include "framework/classes/class1.php";
....
and inside some class files it is including other files in the same way;
what i need is to change the location of the framework files and thus the relative path to the framework files in order to have something like
include "lib/framework/mainfile.php";
but i don't want to look in all the files and change the path in every one of them because i would always have to be doing it again when i change to a new version of the framework.
Is there any way of doing this?
use
set_include_path(implode(PATH_SEPARATOR, array(
realpath('Your/updated/path'),
get_include_path(),
));
so your current include paths will not be effected
and you will not need to make change in all files
You can use __DIR__ to get the absolute path of the directory the current script is in. You can then use:
include __DIR__ . '/classes/class1.php';

How can i make a path file in php

I was wondering how can I make an path file in php?
For example I would like to have a pointer file in the root folder that points to the folder where the php script are held.
I tried something like this, but it does not work.
path.php (is in the root file eg. htdocs/project1/week5)
$path = "/project1/week5/php";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
alert.php (is in the root file eg. htdocs/project1/week5)
include("path.php");
include("AlertFormAction.php");
AlertFormAction.php (is in htdocs/project1/week5/php)
What am I doing wrong? Could somebody be so kind and show me how it is supposed to be done? Thank you.
Your $path variable contains an absolute path that in most likelihood, does not exist.
Paths in PHP are local (to the server) filesystem paths. At a guess, I'd say you want to try
$path = '/htdocs/project1/week5/php';
As the include path "php" seems to be relative to your path.php file, you may find this more flexible
set_include_path(implode(PATH_SEPARATOR, array(
__DIR__ . '/php',
get_include_path()
)));
If using PHP 5.2 or lower, replace __DIR__ with dirname(__FILE__)

Relative include files

I have a file
workers/activity/bulk_action.php which includes a file
include('../../classes/aclass.php');
Inside aclass.php it does:
include ('../tcpdf/config/lang/eng.php');
It seems that the include in the second file is using the first files working directory instead of being relative to itself, resulting in an error. How does this work?
You can adapt the second include with:
include (__DIR__.'/../tcpdf/config/lang/eng.php');
The magic constant __DIR__ refers to the current .php file, and by appending the relative path after that, will lead to the correct location.
But it only works since PHP5.3 and you would have to use the dirname(__FILE__) construct instead if you need compatibility to older setups.
You would be way better off by setting a proper value to the include_path and then use paths relative to this directory.
set_include_path(
get_include_path() .
PATH_SEPARATOR .
realpath(__DIR__ . '/your/lib')
);
include 'tcpdf/config/lang/eng.php';
include 'classes/aclass.php';
I also suggest you take a look at autoloading. This will make file includes obsolete.
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing.
You can use dirname(__FILE__) to get a path to the directory where the currently executed script resides:
include(dirname(dirname(__FILE__)) . '/tcpdf/config/lang/eng.php');
(since PHP 5.3, you can use __DIR__)
Or, define a constant in the first file that points to the root directory and use it in your includes.

PHP including files

What's the difference between
$_SERVER['DOCUMENT_ROOT'];
and
dirname(__FILE__);
I wonder what's the difference because when I 'echo' them, they're returning same path. Which do you prefer should I use and why?
Thanks!
Both are different
_FILE_
The full path and filename of the file. If used inside an include, the name of the included file is returned. 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.
source : PHP magic constants
Let's said, your document is /var/www,
and your index page is /var/www/index.php
dirname(__FILE__) == $_SERVER['DOCUMENT_ROOT'];
But if you drill-down to sub-folder like /var/www/posts/index.php
dirname(__FILE__) != $_SERVER['DOCUMENT_ROOT'];
/var/www/posts != /var/www
The use of $_SERVER['DOCUMENT_ROOT'] is more appropriate in this case.
__FILE__ always points to the current file path, and $_SERVER['DOCUMENT_ROOT'] points to the document root path ;-)
I prefer first one, as it is more semantic.
If you will try to compare the values of the files, that are located not in your docroot - then you'll get different values.
The former one is a root folder for the HTTP server (or VirtualHost) and it is a server setting.
The latter is the folder containing the current file.
The usage is entirely based on requirements in my opinion.
You would normally use $_SERVER['DOCUMENT_ROOT'] when you want to reference your website's root folder from any where within your website or web application.
You will find using dirname(__FILE__) handy if you were including a file, that then needed to include some more files from the same directory. I use this in my PHP wrapper for the Dribbble API
class Dribbble {
function __construct() {
require_once(dirname(__FILE__) . '/base.php');
require_once(dirname(__FILE__) . '/shot.php');
require_once(dirname(__FILE__) . '/player.php');
}
}
This means I can just include dribbble.php from any where in my website or web application and not worry about also including base.php, shot.php, and player.php at the same time.

Categories