Jump to content

PHP absolute paths within a class file


Coder_Mediocre

Recommended Posts

Hi, I'm trying to put some some closure on my understanding of absolute file paths in php. Say I have the following folders with the following files:

 

ROOT/ 'index.php'

ROOT/ INCLUDES/ 'config.php' 'load.php' 'database.class.php'

ROOT/ UPLOADS/ image1.jpg

 

I have 'config.php' in ROOT/INCLUDES which has the absolute path to the application root directory, which I have coded as $config['app_dir'] = dirname(dirname(__FILE__));

 

In all my ROOT folder files, such as index.php, I have the following lines at the beginning of the file, which seems to be working:

 

require_once dirname(__FILE__).'/includes/config.inc.php';

include_once $config['app_dir'].'/includes/load.php';

 

I'm having difficulty understanding how to integrate config.php as an include statement in database.class.php (there are a number of methods in the class of database.class.php called by index.php that build file paths). I've tried excluding the above two lines from the start of database.class.php and simply refer to the files in the class simply as 'uploads/'image1.jpg'. While this works, I'd like to incorporate the above lines for portabilities sake (i.e .$config['app_dir'].'/uploads/image1.jpg.) If I do, however, the browser states $config is an undefined variable.

 

Have I misunderstood the nature of the role of $config['app_dir'] in the config.php file and in actual fact there is no need to refer to the path of image.jpg, as 'uploads/'image1.jpg' will simply do because the database.class has already been called by index.php, where the absolute file path building takes place?

Link to comment
Share on other sites

Say whaattt...

 

I think your confusing what many people refer to as WEB_ROOT and DIR_ROOT. The directory root, which should be used with requires/includes, is commonly /home/some_user/public_html/yourFilesHere. So $config['app_dir'] should really be a full directory including home etc to your application. It looks like you've confused WEB_ROOT with DIR_ROOT?

Link to comment
Share on other sites

Thanks, I had no idea there was a WEB_ROOT that differed from DIR_ROOT.

 

I suppose the question I'm asking is, do I include the config.php file in a class.php file and build file paths from it, or do I continue to refer to paths in a relative sense i.e. 'uploads/image1.jpg.' as the class file is already being included via the loader in the root directory files where $config['app_dir'] already exists?

 

My application works when relative paths are used, but sends out '$config = undefined variable' when I include the config file and try and build the path using $config[app_dir']. Should I just refer to the filepath in a relative sense?

Link to comment
Share on other sites

If your working with classes and objects I recommend taking a look at autoloading. Its the most efficient way of having your classes instantiated on an ad-hoc basis.

 

If a client is accessing images your root (WEB_ROOT) is effectively your domain (http://mywebsite.com/) and you can think of it as your public_html directory. So if you have an images folder in your public_html your absolute path would be "/images/myImage.jpg".

 

If the server is accessing an image you should use the directory root (DIR_ROOT) which is /home/username/public_html/images etc.

 

Bare in mind both constants are defined by you; they aren't a PHP thing. I've calle dthem WEB/DIR_ROOT but you can call them whatever.

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.