Moving between directories, Moving between directories |
Moving between directories, Moving between directories |
SBH |
Mar 22 2016, 12:18 PM
Post
#1
|
Member Group: Members Posts: 82 Joined: 6-February 15 Member No.: 22,158 |
Here's my requirement. This is in PHP.
I've got 2 versions of default.php, both doing processing in slightly different ways. Both need to call another PHP file called startval.php (via include_once). Here is the directory structure. Root/default.php Root/Project/MainDir/SubDir/default.php Root/Project/PHP/startval.php I need a way by which I can invoke startval.php from both the versions of default.php. Preceding the program name, i.e. startval.php (esp for the second version of default.php) with "../../ " ain't working. TIA. This post has been edited by SBH: Mar 22 2016, 12:21 PM |
CharlesEF |
Mar 22 2016, 03:46 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I recommend you always start from the root directory of your web site. This way if you ever move any of the files you don't have to worry about changing any path information. So, I would use '/Root/Project/PHP/startval.php' in your include_once command. The same principal applys to href's in HTML.
|
Christian J |
Mar 22 2016, 03:58 PM
Post
#3
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
An easy way to find the document root is to use
CODE include_once $_SERVER['DOCUMENT_ROOT'] . 'Project/PHP/startval.php'; The above will work even if you move files between a local test server and web server with completely different file paths. See also http://php.net/manual/en/reserved.variables.server.php |
SBH |
Mar 23 2016, 05:36 AM
Post
#4
|
Member Group: Members Posts: 82 Joined: 6-February 15 Member No.: 22,158 |
Gentlemen, thanks for your responses. On trying further, I was able to accomplish using chdir(). And it's sort of working the way we want it to.
Having said that, would you still recommend that I change it to root (the way Christian proposes), so that I do not need to have different codes in the different versions of default.php? Kindly suggest. Thanks once again. |
CharlesEF |
Mar 23 2016, 11:39 AM
Post
#5
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
yes, I would recommend it. I forgot about the $_SERVER['DOCUMENT_ROOT'] command. I also need to use it in a lot of PHP commands but my original example does work with the PHP header command and all HTML href's. If I remember correctly $_SERVER['DOCUMENT_ROOT'] needs the ending / character, so the actual command would be:
CODE include_once $_SERVER['DOCUMENT_ROOT'] . '/Project/PHP/startval.php'; |
SBH |
Mar 24 2016, 12:02 PM
Post
#6
|
Member Group: Members Posts: 82 Joined: 6-February 15 Member No.: 22,158 |
yes, I would recommend it. I forgot about the $_SERVER['DOCUMENT_ROOT'] command. I also need to use it in a lot of PHP commands but my original example does work with the PHP header command and all HTML href's. If I remember correctly $_SERVER['DOCUMENT_ROOT'] needs the ending / character, so the actual command would be: CODE include_once $_SERVER['DOCUMENT_ROOT'] . '/Project/PHP/startval.php'; Thanks. Will try both with & without the ending '/' character & revert, if need be. Thanks again to both of you. Regards |
SBH |
Mar 25 2016, 02:54 AM
Post
#7
|
Member Group: Members Posts: 82 Joined: 6-February 15 Member No.: 22,158 |
Now, this is silly I know, but how'd I link to the same JS & CSS style sheets from different versions of default.php. I currently make reference to parent directories (one or more) by ../../ et al.
And yes, the $_SERVER command did work, with the ending '/' character. Thanks This post has been edited by SBH: Mar 25 2016, 02:58 AM |
CharlesEF |
Mar 25 2016, 05:18 AM
Post
#8
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
You do that the same way I suggested in my first post.
CODE /Root/Project/JS/somescript.js or /Root/Project/CSS/somecss.css Just be sure to put the correct path and file name in. |
Lo-Fi Version | Time is now: 23rd April 2024 - 09:41 PM |