Page 1 of 1

Issue with using __DIR__

Posted: Tue Jan 22, 2013 2:10 pm
by Pazuzu156
I'm using the __DIR__ constant to return the directory for the file i'm in to include something. However say I use it, I'm returned with C:\xampp\htdocs\myapp\assets instead of /myapp/assets

When I do this, I'm returned with nothing instead of the file I'm trying to add to my page. How do I fix this?

If you didn't catch it from the path, I'm using Windows.

Re: Issue with using __DIR__

Posted: Tue Jan 22, 2013 2:51 pm
by requinix
"/myapp/assets" is not a directory, it's a URL. The fact that it looks like a directory is just because URLs look like file paths. Even though they often map to files, URLs are not file paths.
If you're comfortable with __FILE__ then think of __DIR__ being the exact same thing as dirname(__FILE__).

First look in $_SERVER to see if there's anything relevant in there, like SCRIPT_NAME or SCRIPT_URI or REQUEST_FILENAME.

Otherwise if you don't know what file you're writing code in (!?) then you can use a combination of substr(), __DIR__, and the DOCUMENT_ROOT:

Code: Select all

$path = str_replace("\\", "/", substr(__DIR__, strlen($_SERVER["DOCUMENT_ROOT"])));

Re: Issue with using __DIR__

Posted: Tue Jan 22, 2013 3:11 pm
by pickle
If you're include()ing something, why doesn't the absolute path work? Or do you mean you're using __DIR__ client-side and using it to reference JS/CSS/images?

Re: Issue with using __DIR__

Posted: Tue Jan 22, 2013 4:52 pm
by Pazuzu156
I'm using it for including stylesheets. I've used $_SERVER but I'm exploring other ways of doing the same thing.

Re: Issue with using __DIR__

Posted: Wed Jan 23, 2013 6:37 am
by twinedev
What I usually do is have a main config file that all scripts include, and these set up constants to use for things. Example:

Code: Select all

<?php 
  define('CUR_TIME',time());
  require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/settings.php'); 
?>

<a href="<?php echo ADMIN_URL; ?>login">Admin Login</a>
then in settings.php:

Code: Select all

<?php
  if(!defined('CUR_TIME')) { die ('Invalid direct call of script'); }

  session_start();

  define ('SITE_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
  define ('SITE_URL', '/');
  define ('ADMIN_PATH', SITE_PATH.'admin');
  define ('ADMIN_URL', '/admin');

  // Other items...
?>
Now if I need to port this over to another location on a server besides the root of the domain to say inside the directory mysite, I can do a search/replace across all files for the site and change
[text]$_SERVER['DOCUMENT_ROOT'].'/[/text] to [text]$_SERVER['DOCUMENT_ROOT'].'/mysite/[/text] (and also update the SITE_URL listings in the settings.php script)

The reason I define the constant CUR_TIME is that I use it in many places in my scripts, and this gives you a constant that all included files should check for to make sure you are not directly calling them (first line in settings.php)

Just a way I like to do it, there are others.... (note, if a script is being called directly to run as a cron that includes settings.php, do NOT use $_SERVER['DOCUMENT_ROOT'], as it will not exist)

Re: Issue with using __DIR__

Posted: Thu Jan 24, 2013 9:11 pm
by Pazuzu156
I was playing around with requinix's shot at it along with twinedev's defining it, and came up with:

Code: Select all

<?php

    define('_DIR_', dirname(str_replace("\\", "/", substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT'])))));

?>
I come out with /mysite

So I just add /<file_name_i_need> and it worked fine.

Re: Issue with using __DIR__

Posted: Thu Jan 24, 2013 10:16 pm
by requinix
I'm wondering why relative paths won't work, but

As a critique can you use a different constant name? It looks too much like __DIR__ and the underscore thing is supposed to be for PHP's constants only. Or you could put it in $_SERVER where lots of other similar "constants" are.

Re: Issue with using __DIR__

Posted: Thu Jan 24, 2013 11:32 pm
by Pazuzu156
I don't intend to use this constant on anything I publish, I just used it as a base for what I came up with.