Why the paths are hardcoded ? For example:
require_once 'Zend/Gdata/Extension.php';
The thing is that Zend folder is not in root - it's in library/packages
So do I have to change the path of every file?
To be like :
define('ROOT_PACKAGES', dirname(__FILE__) . "/library/packages/");
ROOT_PACKAGE.'Zend/Gdata/Extension.php';
Let me explain by asking you a question. When you use 'Zend/Gdata/Extension.php' - what directory is it looking in for the sub-directory Zend? You will answer "/library/packages/" and that leads to the key question. Why?
Update: My question is not why use that directory, it's why would the O/S have that directory as the default directory.
At any point in time there is a default directory. And relative paths will then be evaluated using that default directory. But the value of that is not locked to a specific directory. And it is dependent on how an app is running. I assume you have a web app and basically any code in your app, or the web server, can change the directory.
So yes, use ROOT_PACKAGES. You should always use fully qualified directories.
Related
I want to refer to my website root, or more exactly, to the directory above my script's one.
Let's say my website is example.com/test. I made a installation site which writes a config file. But it shouldn't write it to example.com/test/install/config.php, but to example.com/test/config.php. And the biggest pain in the ** is that I run on Windows (my development PC).
How do I do it?
If you want to get the web-site document root, you can use:
$_SERVER['DOCUMENT_ROOT']
That should work regardless of the operating system and gives you a path on the local file system (so no www.etcetc.).
If you want to get the fully-qualified path of your site root, from that file it is:
$root = realpath(dirname(__FILE__) . '/../../..');
The double-dots work their way up the directory structure, then realpath() is used to turn it into a proper path. So if you want to traverse one less folder up, use two sets of dots rather than three.
I created initialize.php to define my project's root directory.
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT') ? null : define('SITE_ROOT', DS.'works'.DS.'myproject');
I include it with something like this from any directory.
require_once("../includes/initialize.php");
and I include other files easier. I am going to upload the files to server, then I should only change the initialize.php file.
It works. But, when I use user friendly URL's, and call ajax files from it, I don't know my directory hierarchy to include initialize.php.
I don't want to do this:
require_once("/works/myproject/includes/initialize.php");
for every file. Because it is going to change when I upload everytime.
I don't want to use session or cookie for everyuser.
There should be a trick for this but I couldn't find. Is there a way for getting project root from anywhere while using user friendly URL's and ajax calls?
I fixed it.
when I call it with ajax it's ok. But I included it as php too for some conditions.
Because of current and the ajax files are in different folders, it crashed.
So, when I change it to only require initialize.php when called with ajax, problem solved.
If you're using Apache, you could try adding your includes directory to your include_path directive.
Find the current value by doing a phpinfo() and look for include_path and then try re-declaring it in your top-level web directory and add the absolute path to your includes directory.
So if the default value is:
include_path = ".:/usr/bin/php:/some/other/directory"
just copy-paste the same thing in your .htaccess file but add your include directory on the end, separating it with a colon.
include_path = ".:/usr/bin/php:/some/other/directory:/works/myproject/includes"
and that way you shouldn't have to reference the absolute path every time.
This all depends on the permissions your web host gives you. There are other (easier) ways to do this, but I find that most of them are usually restricted by hosting providers, and manually setting it via .htaccess is usually the most dependable way to get this done.
Here's a page listing a few different ways: Five ways to create include path for PHP
Simply doing this:
require_once("../includes/initialize.php");
is enough because PHP doesn't look at your friendly URLs. It includes whatever you give him to include relative to your script.
Also there is $_SERVER['DOCUMENT_ROOT'] that will give you an absolute path from your root directory.
It is a good approach to define all your directories in a common file as you added initialize.php. You will have to include this common file in every file on the project. User friendly url's have no effect on the file inclusion.
Is this, for example, a good idea?
require_once($_SERVER['DOCUMENT_ROOT'].'/include.php');
If you have two virtual hosts on the same server, one for live and one for development, with different Apache DocumentRoots, this would avoid having to include absolute paths when the source of the include is unknown, and may be in any directory.
(Note: file paths in the following section are relative to the web root. They would in fact be like /var/www/app/core/init.php, where /var/www/app is the web root)
For instance: I have an /core/init.php which is called using relative paths from places all over the website (/file.php, /dir/file.php or /dir/dir/file.php).
This init.php then includes several function pages, in the fund directory, a subdir of /core (as in /core/func/userfunctions.php).
So, in init.php, I can use the $_SERVER method, because it breaks if I use a relative path and try to call functions from a page like /dir/file.php.
I can't see any problem with it, but in general what could go wrong?
I've seen cases where $_SERVER['DOCUMENT_ROOT'] is not set or is not what you would expect (i.e. not set in CLI or old IIS, or invalid in certain CGI setups).
For that reason you can use dirname(__FILE__) to obtain the path of the script that line is called in. You can then reference relative paths from there e.g.
include dirname(__FILE__) . '/../../other/file.php';
I go with the above method when the directory structure of the files is known and is not subject to change.
If DOCUMENT_ROOT is not available, the following is a suitable replacement:
substr($_SERVER['SCRIPT_FILENAME'], 0, -strlen($_SERVER['SCRIPT_NAME']));
You don't need to do this. PHP looks for the included file in the document root by default.
You can use set_include_path($new_include_path) to change this behaviour, or edit include_path in the php config file.
Also, from http://php.net/manual/en/reserved.variables.server.php:
'DOCUMENT_ROOT'
The document root directory under which the current script is executing, as defined in the server's configuration file.
For example, if you use URL rewriting, you will be very happy when you find out that the includes in your /there/are/so/many/paths/in/the/url/of/this/ page are still working!
I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here
I'm having problems with my include files. I don't seem to be able to figure out how to construct my URLs when I use require_once('somefile.php'). If I try to use an include file in more than one place where the directory structures are different, I get an error that the include file cannot be found.
In asp.net, to get my application root path, I can use ~/directory/file.aspx. The tild forward slash always knows that I am referencing from my website root and find the file no matter where the request comes from within my website. It always refers back to the root and looks for the file from there.
QUESTION: How can I get the root path of my site? How can I do this so I can reuse my include files from anywhere within my site? Do I have to use absolute paths in my URLs?
Thank you!
There is $_SERVER['DOCUMENT_ROOT'] that should have the root path to your web server.
Edit: If you look at most major php programs. When using the installer, you usually enter in the full path to the the application folder. The installer will just put that in a config file that is included in the entire application. One option is to use an auto prepend file to set the variable. another option is to just include_once() the config file on every page you need it. Last option I would suggest is to write you application using bootstrapping which is where you funnel all requests through one file (usually with url_rewrite). This allows you to easily set/include config variables in one spot and have them be available throughout all the scripts.
I usually store config.php file in ROOT directory, and in config.php I write:
define('ROOT_DIR', __DIR__);
And then just use ROOT_DIR constant in all other scripts.
Using $_SERVER['DOCUMENT_ROOT'] is not very good because:
It's not always matching ROOT_DIR
This variable is not available in CGI mode (e.x. if you run your scripts by CRON)
It's nice to be able to use the same code at the top of every script and know that your page will load properly, even if you are in a subdirectory. I use this, which relies on you knowing what your root directory is called (typically, 'htdocs' or 'public_html':
defined('SITEROOT') or define('SITEROOT', substr($_SERVER['DOCUMENT_ROOT'], 0, strrpos($_SERVER['DOCUMENT_ROOT'], 'public_html')) . 'public_html');
With SITEROOT defined consistently, you can then access a config file and/or page components without adapting paths on a script-by-script basis e.g. to a config file stored outside your root folder:
require_once SITEROOT . "/../config.php";
You should use the built in magic constants to find files. __FILE__ and __DIR__. If you are on PHP < 5.3 you should use dirname(__FILE__)
E.g.
require_once __DIR__.'/../../include_me.php';
$_SERVER['DOCUMENT_ROOT'] is not always guaranteed to return what you would expect.
Define it in a config file somewhere.
Assuming you're using an MVC style where everything gets routed through a single index.php then
realpath('.');
Will show you the path to the current working directory (i.e where index.php is)
So then you can define this as
define('PROJECT_ROOT', realpath('.'));
If it's not MVC and you need it to work for files in subfolders then you can just hard code it in a config file
define('PROJECT_ROOT', 'C:/wamp/www/mysite');
Then when including something you can do;
include PROJECT_ROOT . '/path/to/include.php';
You could alternativly set the base directory in your .htaccess file
SetEnv BASE_PATH C:/wamp/www/mysite/
Then in PHP you can reference it with $_SERVER['BASE_PATH']
Try this:
$_SERVER['DOCUMENT_ROOT']