Jump to content

Getting Root Path To Include Files


sairfan1

Recommended Posts

hi, im developing a web site, which include many folders, i have a common folder its path is files/common/phpmy project files are out side the file folder, with contains more folders it become very hard to give path like ../../../../files/common/php on each page, is there some way to get root path that i can add same path to every page ie<? require("~/files/common/php/connection.php"); ?>thanks

Link to comment
Share on other sites

Is the connection file in this place all the time?

files/common/php/connection.php
You could use this in the top of each page that needs the connection.php file;
<?php require("path/to/your/file/you/want/to/link/to"); ?><?php require_once("path/to/your/file/you/want/to/link/to"); ?>

http://www.w3schools.com/php/php_includes.aspRemember this;

include() = very resource hungry with heavy server load and low memoryrequire() = much much less load on the server
Link to comment
Share on other sites

Remember this;
include() = very resource hungry with heavy server load and low memoryrequire() = much much less load on the server
Do you have any reliable sources to back that up?
Link to comment
Share on other sites

require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.
http://www.php.net/manual/en/function.require.php
if you try to load a flat file database with the include(), it'll take longer to load than require().
That's not true.
Link to comment
Share on other sites

im calling from this pathroot/folder1/folder2/test.phpconnection.php is located inroot/common/php/connection.phpi have many other files and foldes likeroot/folder1/folder3/*.phproot/folder2/folder1/folder1/*.phpi want the same path string for all pages,, for example naturally on some pages it may look like, require("../../common/php/connection.php") or may b require("../common/php/*.php") is it possible to add standard one string same for all pages??

Is the connection file in this place all the time?You could use this in the top of each page that needs the connection.php file;
<?php require("path/to/your/file/you/want/to/link/to"); ?><?php require_once("path/to/your/file/you/want/to/link/to"); ?>

http://www.w3schools.com/php/php_includes.aspRemember this;

Link to comment
Share on other sites

Thanks, good idea,but in case i use same project on some different domain, i will have to change path on every page, in fact i was looking for code to use once for every whereplease advise if we can further improve it.

Place this code in a file called getdirectory.php
<?php// current directoryecho getcwd() . "\n";?>

Place that file in the folder where your connection.php is located, Run the fileUse what it gives you.

Link to comment
Share on other sites

i will have to change path on every page, in fact i was looking for code to use once for every whereplease advise if we can further improve it.
What you talking about???
Your connection string will change according to your database needs.That code I posted only gets you a path, the path to the file you need to include on each of your pages
Link to comment
Share on other sites

yes my connection string will remain same, im talking about path that i will give in required("") function, lets take an examplefile is located inwww.example.com/files/common/php/connection.phpmy web pages are stored inwww.example.com/pages/folder1www.example.com/pages/folder2/folder1www.example.com/pages/folder2/folder3/page1.php and as on... in file page1.php i added path as advised by you, suppose that is, c:\sites\files\common\php\connection.php, rite?i pasted same path all over pages on my web site, some other time i use same code for some different web site, where root path is f:\public\clients\websites\files\common\php\connection.php so i will have to replace path on all pages??it would be ideal if some how i can get root path that is c:\sites\ or in other case f:\public\clients\websites\

What you talking about???
Link to comment
Share on other sites

The easiest way may be to add your site's root folder to the include path for PHP. You can use this function to set the include path:http://www.php.net/manual/en/function.set-include-path.phpIf you add your root directory to the path, then you can include files relative to that directory. So if you add "c:\sites" to the include path, then you could just use this to include your files:include 'files/common/php/connection.php';As long as the root folder is part of the include path, that statement would work from any file on your site.Personally, for most of my applications I just define the root directory as a configuration variable. I define an array and add whatever information I want to it. This is my $config variable for one application:

$config = array(  'db_host' => 'localhost',  'db_user' => 'root',  'db_pass' => '1234',  'db_name' => 'lms7_base',  'http_root' => 'http://www.domain.com/lms7',  'lms_title' => 'LMS7', // also defined in lms.js  'cookie_domain' => '.domain.com',  'ext_theme' => 'slate',  'ds' => DIRECTORY_SEPARATOR,  'lms_root' => dirname(dirname(__FILE__)),  'rtf2pdf' => '/usr/local/Ted/rtf2pdf.sh',  'fname_replace' => array(' ', "\n", "\r", "\t", '(', ')', '&', '#', '?', '=', '@', ':', '%', '"', "'", '[', ']', '{', '}', '+'),  'cache_timeout' => 15, // time in minutes for items in the cache to be valid  'image_upload_rel' => DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . 'images',  'file_upload_rel' => DIRECTORY_SEPARATOR . 'resources',  'ug_css_rel' => DIRECTORY_SEPARATOR . 'custom',  'timezone' => 'US/Arizona', // see http://www.php.net/manual/en/timezones.php  'timestamp' => 'n/j/Y g:ia', // see http://www.php.net/manual/en/function.date.php  'datestamp' => 'n/j/Y',  'time' => 'g:ia',  'news_datestamp' => 'l F jS, Y',  'content_types' => array(	0 => 'Unknown',	1 => 'Online Training',	2 => 'Classroom Training',	3 => 'Online Test/Questionnaire',	4 => 'Other Online Resource',	5 => 'Live Online',	'' => 'Unknown'  ),  'allowed_cert_ext' => 'rtf,pdf',  'allowed_image_ext' => 'jpg,jpeg,gif,png',  'allowed_package_ext' => 'zip',  'thumbnail_w' => 256,  'thumbnail_h' => 192,  'news_image_w' => 224,  'news_image_h' => 480,  'ug_banner_h' => 50,  'api_enc_key' => md5('the secret LMS API key'));

So that defines an element called $config['lms_root'], which stores the path to the root directory of the application. This file uses dirname to set that, so I don't have to manually set the path every time I move or install a new application, it just always sets the root path to the parent directory of that file (that file would be in the /include directory, so the root is one level up).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...