...

View Full Version : Basepath variable



kleidi
02-21-2010, 03:19 PM
Hello everyone!

I'm creating a miniscript for user management but i have some issues:
When i want to include a file, i should everytime add the fullpath to be sure that the file is included correctly and i was thinkging if i can do this via any variable like :

$bpath = '/home/userpanel/public_html/anydir/';

and when i need to use it anywhere, to do something like:

include '.$bpath. anyotherdir/athordir/anyfile.php'
?????

Is this possible in any way?

I have done it with domain:

$domain = 'http://domain.tld/anydir/otherdire/';

and when i use it i do something like:

header('Location: '.$domain.'index.php?error=1');

Hope that i was understandable ;) and someone can help me.

Thank you in advance!

SKDevelopment
02-21-2010, 03:24 PM
Yes, this is quite common practice. I would define path constants in a configuration file like


define('BPATH','/home/userpanel/public_html/anydir/');

and include the configuration file where necessary. It would allow you to reconfigure the system quickly if you e.g. move to a new hosting.

In the same way you could define paths to the most important folders of the system which you could need in the code.

I prefer defining paths as constants because constants are available in any scope (global scope, function scope, class scope etc.) after they are defined.

kleidi
02-21-2010, 04:04 PM
Yes, this is quite common practice. I would define path constants in a configuration file like


define('BPATH','/home/userpanel/public_html/anydir/');

and include the configuration file where necessary. It would allow you to reconfigure the system quickly if you e.g. move to a new hosting.

In the same way you could define paths to the most important folders of the system which you could need in the code.

I prefer defining paths as constants because constants are available in any scope (global scope, function scope, class scope etc.) after they are defined.

Thank you.
I added it at the config file, but, how to use that constant now?
thanks again ;)

SKDevelopment
02-21-2010, 04:17 PM
I usually define all path constants in a configuration file e.g. similar to this:


define('BPATH','/home/userpanel/public_html/anydir/');
define('BPATH_INCLUDE',BPATH . 'include' . DIRECTORY_SEPARATOR);
define('BPATH_JS',BPATH . 'js' . DIRECTORY_SEPARATOR);

Now the only file which I need to include by relative path is this configuration file. Other files are included or accessed by absolute paths defined by the constants. E.g.


require_once(BPATH_INCLUDE . 'myclass.inc.php');

kleidi
02-21-2010, 04:33 PM
Warning: require_once(BPATH_INCLUDEadmini/include/dbconfig.php) [function.require-once]: failed to open stream: No such file or directory in E:\Program Files\VertrigoServ\www\live\admini\modulet\ndeshje\shfaqndeshje.php on line 11

Fatal error: require_once() [function.require]: Failed opening required 'BPATH_INCLUDEadmini/include/dbconfig.php' (include_path='.;E:\Program Files\VertrigoServ\Smarty') in E:\Program Files\VertrigoServ\www\live\admini\modulet\ndeshje\shfaqndeshje.php on line 11

Didn't worked for me :(

SKDevelopment
02-21-2010, 04:57 PM
No, you would need to include the configuration file with constants by relative or absolute path. E.g. by relative path it could be like this:


require_once('include/configure.php')

Only after this you would be able to use the constants.

Inigoesdr
02-21-2010, 05:25 PM
kleidi, keep in mind that this is a CONSTANT, not a STRING. You use it just like you would a variable, meaning don't wrap it in quotes. Constants are also available globally, which is handy for dynamic paths. See the manual page (http://php.net/language.constants.syntax) for more information.

kleidi
02-21-2010, 06:56 PM
Got the idea ... thank you very much. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum