Jump to content

Find where the application is installed?


THEK

Recommended Posts

Hi,

 

I'm working on a project which was going pretty well until I discovered the application doesn't actually know where it is installed.

 

So far when I want to include the config file I just use

 

$_SERVER['DOCUMENT_ROOT'].'/config.php';

 

Which works fine if the application is installed in the root directory. But how do I get the folder the application is installed in? e.g.

 

Document Root Folder: C:\Web\

Actually Application Folder: C:\Web\MyApp\

 

 

How do I get the \MyApp dynamically? I can't just use './' all the time because I have files accessed by AJAX stored in other folders.

 

Thanks in advance.

Link to comment
Share on other sites

dirname(__FILE__);

 

I have a config file that contains database information and some constants. This is generally included in every page. Including one's which are accessed via an AJAX request.

 

My problem is I've only been testing on my website with the application running from the root directory so I was including the config file from all files like so:

 

PHP Code:

require_once $_SERVER['DOCUMENT_ROOT'].'/config.php'; 

This has a problem, as I picked up on in the first bit of testing, if someone installs it to say:

WEBROOT/myApp/

 

The config file will be unreachable.

 

My question is what is the easiest way to get around this? If I didn't use ajax I wouldn't need to worry about this because I could set the require statement in the main file and all the other includes would pick up on it.

 

However, because I do use AJAX there are files within folders that would need different levels of dirname before getting to the right one. I was thinking of storing the root directory in a hidden input and sending that as data with the AJAX but is this the best solution?

Link to comment
Share on other sites

dirname(__FILE__); gives you the directory to the file. So if everything is running out of your index.php in the webroot, then all you to do is put this function in front of every include.

 

It doesn't matter if the webroot changes, because it will still give the path to the file that called it.

 

AJAX has no bearing on where files are.

Link to comment
Share on other sites

dirname(__FILE__); gives you the directory to the file. So if everything is running out of your index.php in the webroot, then all you to do is put this function in front of every include.

 

It doesn't matter if the webroot changes, because it will still give the path to the file that called it.

 

AJAX has no bearing on where files are.

 

I tried that but it looks messy when I'm within several folders. dirname(dirname(dirname(dirname(__FILE__))));

 

Just use a relative path from your file to the included file for your config include.  The files' relative locations to each other shouldn't be changing.

 

require '../../config.php';

 

When I do ../../config.php; it can't find it. But if I do ././config.php it can. Is that right?

 

**EDIT** Found the problem. This is where I was talking about ajax issues.

I use the same file through both AJAX and includes. I include it when the page loads and use ajax when they want to refresh it or a change is made.

 

If I access it through AJAX I have to use

 

../../config.php

 

But when including it I have to use

 

././config.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.