Issue with using __DIR__

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Issue with using __DIR__

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Issue with using __DIR__

Post 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"])));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Issue with using __DIR__

Post 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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Issue with using __DIR__

Post by Pazuzu156 »

I'm using it for including stylesheets. I've used $_SERVER but I'm exploring other ways of doing the same thing.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Issue with using __DIR__

Post 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)
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Issue with using __DIR__

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Issue with using __DIR__

Post 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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Issue with using __DIR__

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply