Jump to content


Photo

PHP absolute paths within a class file


  • Please log in to reply
3 replies to this topic

#1 Coder_Mediocre

Coder_Mediocre

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 08 January 2013 - 05:27 PM

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?

#2 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 08 January 2013 - 07:00 PM

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?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."

#3 Coder_Mediocre

Coder_Mediocre

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 09 January 2013 - 04:50 AM

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?

#4 cpd

cpd

    ¬_¬

  • Members
  • PipPipPip
  • 881 posts
  • LocationLondon, UK

Posted 09 January 2013 - 07:21 AM

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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

"One of my most productive days was throwing away 1000 lines of code."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com