Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to add ../ for each dir from root?
Old 09-04-2007, 11:57 AM how to add ../ for each dir from root?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hey, how can i count how many /somthing/soemthin/thispage there are and add a ../ to it

this is for include() so the path is correct.

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 09-04-2007, 06:54 PM Re: how to add ../ for each dir from root?
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Why not try a site relative path instead:

./ (site root)
./oneFolderIn/twoFoldersIn/
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 06:56 PM Re: how to add ../ for each dir from root?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
i though ./ is the dir root?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-04-2007, 07:06 PM Re: how to add ../ for each dir from root?
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
Why not do it the easy way Have a folder for included files and then
include($_SERVER['DOCUMENT_ROOT']."/phpIncludes/file_to_include");
joder is offline
Reply With Quote
View Public Profile
 
Old 09-04-2007, 07:34 PM Re: how to add ../ for each dir from root?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
i though ./ is the dir root?
No !
/ is the root
./ is the current dir
../ is one dir up the tree

You can even test it on windows, it just use the backslash rather than the slash.
start -> run -> cmd

dir .\ => c:\Documents and settings\your username
dir ..\ => c:\Documents and settings
dir \ => c:
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-04-2007 at 07:36 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-04-2007, 09:12 PM Re: how to add ../ for each dir from root?
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
well - my bad. I only trust relative paths when all pages are in root.

I use config file that toggles the site between local, test, and live server modes. Each mode has a different site root. So to combat rel paths getting fricked up for includes/links, I include a var $ROOT in the mode switch:

Script: /includes/inc_config.php
PHP Code:

$env 
'local';

switch (
$env) {

  case 
'local':
  
$ROOT 'http://localhost/sitefolder/public_html/';
  break;

  case 
'test':
  
$ROOT 'http://123.45.67.89/~siteUsr/';
  break;

  case 
'live':
  
$ROOT 'http://www.siteDomain.com';
  break;


Only one absolute/relative path to the inc_config.php is required for each script, subsequent paths/links can be referred to as: $ROOT.'folder/file.php';
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 10:02 PM Re: how to add ../ for each dir from root?
Ultra Talker

Posts: 483
Trades: 0
I agree that you should ALWAYS include via absolute paths (not just a trust issue... there are problems with how PHP deals with nesting includes across directories).

Having said that, I have to disagree with metho's implementation, for two reasons:
1) He's including via HTTP as opposed to the filesystem. 99% of the time this is the wrong thing to do.
2) It's too complicated! You have to track the servers and always make sure that the $env variable is set correctly.

Me, I use dirname( __FILE__ ) and append a relative path to the end of that.

So, if the file I wanted to include was called 'config.php' and was located up two directories in a subdirectory called 'includes', I would use:
PHP Code:
include_once( dirname__FILE__ ) . '../../includes/config.php' ); 
When the include_once is actually called, it's given an absolute path but I get the convenience of using relative paths. Plus, it works no matter what server I'm on at the time, no matter what directory is my 'app root'. Finally, it fixes the problems that I mentioned right at the start regarding nested includes...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-04-2007, 10:59 PM Re: how to add ../ for each dir from root?
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
You know, ur right; going back through the past few sites, I'm only using $ROOT for links and paths to media/images.

Since I started this system, I use inc_confic.php as the only include required to do all basic operations. inc_config.php uses relative paths to remaining includes that are required for the site to run.

Too complicated?? jebus!

The mode switch holds values for the db, root, default email addresses and whatever else is constant across the site. Which would you prefer to do, change $env to 'test' to toggle the site config, or manually change all the vars?

Bloody simple and saves time.

p.s. dirname(__FILE__) works well; I like it.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 11:14 PM Re: how to add ../ for each dir from root?
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
I don't ever use relative paths. I don't understand why anyone would want to go ../../ I'm in a different directory now ../

I use php header and footer files for my site design and with absolute paths I have the same two lines in every file. Then I can totally redo a site edit three files: header, footer and css.
joder is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 08:52 AM Re: how to add ../ for each dir from root?
Ultra Talker

Posts: 483
Trades: 0
metho:

I guess I should clarify what I mean by 'complicated'. I agree that it's a good way to go, for sure, but for my liking it could be a bit more 'automatic'. In fact, I used to do the same thing that you do, but found that I was forgetting to change the 'super flag' too many times and then getting weird errors.

Nowadays I do one of two things:
1) just make sure that I never copy over the config file... I have a habit of 'packaging' anything I'm about to upload, anyway, so I spot the rogue config file pretty quickly.
2) have various config files, all named according to the server name.

Just to expand on (2) there. In your example, instead of 'local', 'test' and 'live' , I would have three config files: 'localhost.config.php', '123.45.67.89.config.php' and 'www.siteDomain.com.config.php'. Then when the file is accessed I would grab the server name, prepend it to 'config.php' and then go from there. Basically the same concept as yours, but a little more automatic: I don't need to change any variables, which I like

In the case of 'www.siteDomain.com.config.php' I might also have a 'siteDomain.com.config.php' file, just in case, that would simply include 'www.siteDomain.com.config.php'.

Having said all that, 95% of my work these days is CMS plugins, so it's not that much of an issue for me anyway.

Again, I would just like to reiterate that there are problems with including via relative paths: normally it won't be an issue but let me tell you.... when you hit it, it sucks.

Instead of rewriting it, let me just copy and paste what I said at http://forum.joomla.org/index.php/to...html#msg278278 (I'm Narcissus over there, for what it's worth).
Quote:
As an example, imagine this really poorly drawn directory structure:
DIR_A: fileA
: fileB
: DIR_A_B: fileAB
: fileB
: DIR_A_B_C: fileABC


If fileAB includes '../fileB' and fileABC includes '../fileAB' then fileABC will actually end up including fileB from the DIR_A_B directory, even though the included fileAB references the fileB in DIR_A.

This is because the include effectively copies the code directly in to the file. Once it's there, the include of '../fileB' references the subdirectory DIR_A_B.

Hence, including files with relative paths is a bad idea, as then you can't just include files all across the code base.
It may seem like an unusual situation but if you had a config.php file in your root for your web app and then another config.php file in a subdirectory for a plugin (for example) then things may get hairy.

As far as I'm concerned it's a bizarre issue as in theory, I would have thought that the dirname( __FILE__ ) solution would suffer the same fate. It doesn't and I don't know why... I daresay the issue will be fixed eventually, but until then, that's why I do the dirname thing (which I'm glad to hear worked).

BTW: I don't want you to think I'm disagreeing with you. Your approach is definitely a good one (and a lot better than many of the PHP hacks on this forum... all present company excluded ), but I just thought I'd share what I've done in an effort to not have to worry about forgetting to change things.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 02:34 PM Re: how to add ../ for each dir from root?
Extreme Talker

Posts: 182
Trades: 0
Right now I have a config file with something like this:

PHP Code:
$_CFG['ROOT_DIR'] = $_SERVER['DOCUMENT_ROOT'] . 'sitefolder/'// root dir
$_CFG['TPL_DIR']  = $_CFG['ROOT_DIR'] . 'tpl/'// templates dir 
Then in a file I might do:

PHP Code:
require_once ( $_CFG['TPL_DIR'] . 'mytemplate.tpl' ); 
I have not had any problems yet.

Is this a bad way to do things?
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 09-05-2007, 03:40 PM Re: how to add ../ for each dir from root?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Absolutely not.
I use something a bit different, with defines.
That way, I know I cannot change my path accidently.

PHP Code:
define('DOC_ROOT',$_SERVER['DOCUMENT_ROOT']);
define('TPL_ROOT',DOC_ROOT."../smarty/"); 
And I do my includes
PHP Code:
include_once(DOC_ROOT."/libs/auth/clAuth.php"); 
through my code.

I ended with a pretty complete framework of functions that I carry from sites to sites.
Pretty useful.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-05-2007, 03:45 PM Re: how to add ../ for each dir from root?
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
TwistMyArm, I tried the multiple config file approach a few years back, just as you describe. I found it cluttering up my config folder and added unnecessary steps to confirm which mode the config.php was in:

config.php
local.config.php
test.config.php
live.config.php

So I changed to the switch method and a single config file. Being able to just change a single var is way more convenient.

Insofar as the topic is concerned though, I previously used $_SERVER['DOCUMENT_ROOT'], but it is not supported if the directive in php.ini is blank. I would advise against it.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-05-2007, 04:57 PM Re: how to add ../ for each dir from root?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
oookay sorry i dont have time to read all the replies. but i am sing soemthing like what someone above said.

im usign $core for the domain. (becaused eventually it will be changed)

my problem is, im now using mod_rewrite and it seems to be messing up my include file paths. so i wanted it to be able to work but i dont know it im all confused now

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to how to add ../ for each dir from root?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.83116 seconds with 12 queries