I seem to misunderstand the usage of __PHP__ constant

Hello to you all,
I have a problem understanding how to use DIR constant

Here is the code of the file main.php .All works well

_<?php_
_if(!isset($_POST['firstname'])){_
_	include __DIR__ .'/../../form.html.php';_
_}else {_
_	$firstname = $_POST['firstname'];_
_	$lastname = $_POST['lastname'];_

_	if ($firstname =='Kevin' && $lastname == 'Yank'){_
_	$output = 'Welcom, Oh glorious leader!';_
_} else {_
_	$output = 'Welcome to our website, ' .htmlspecialchars($firstname, ENT_QUOTES, 'UTF-8') .' ' . htmlspecialchars($lastname, ENT_QUOTES, 'UTF-8') . '!';_
_	_
_}_

_include __DIR__ .'/../welcome.html.php';_
_}_
_?>_

Here is the code for form.html.php

_<!DOCTYPE html>_
_<html>_
_ <head>_
_  <title> Enter your name </title>_
_  <link rel="stylesheet" href="form.css" />_
_  <meta charset="utf-8">_
_ _
_ </head>_

_ <body>_
_  <form method="POST" action="">_
_   <label for="firstname">First name:</label>_
_	<input type="text" name="firstname"id="firstname" />_

_	<label for="lastname">Last name:</label>_
_	<input type="text" name="lastname"id="lastname" />_

_	<input type="submit" value="GO">_
_</form>_
_ </body>_
_</html>_

Here is the code for welcome.html.php

_<!DOCTYPE html>_
_<html>_
_ <head>_
_  <title> Form Example </title>_
_  <meta charset=""utf-8">_
_</head>_

_ <body>_
_	<p><?php echo $output;?></p>_
_ </body>_
_</html>_

If I understand correctly, the DIR “knows” where the including file is located so that if I move the file to a new directory it should work without updatind the code.
Yet, when I created a newdirectory and I cut and pasted the file there. I didn’t work

I got these messages:
Warning: include(C:\wamp\www\PracticePhP\templates\PHPPRA\STILL/…/…/form.html.php): failed to open stream: No such file or directory in C:\wamp\www\PracticePhP\templates\PHPPRA\STILL\main.php on line 3

And

Warning: include(): Failed opening ‘C:\wamp\www\PracticePhP\templates\PHPPRA\STILL/…/…/form.html.php’ for inclusion (include_path=‘.;C:\php\pear’) in C:\wamp\www\PracticePhP\templates\PHPPRA\STILL\main.php on line 3

Line 3 is where the 1st include is.

I work on wamp at home.
It isn’t a problem of pesmissions

How can I take advantage of thr DIR constant if I move the including fileand the code fails

__DIR__ refers to the directory the file that uses __DIR__ is in. It is useful in case you ever need to run your code a different machine and your code is placed in a different directory.

Instead of _DIR\ I used to use getcwd(); which gives the same output. The new PHP magic constant is preferred rather than use the function call.

http://php.net/manual/en/language.constants.predefined.php

getcwd is quite tricky. It can give you the correct answers,but there are no guarantees.

Before __DIR__ existed most people used dirname(__FILE__), which has the same result as __DIR__.

1 Like

You need to be using the proper directory separator for the system you are on. Taking into account that your local dev environment will probably be different than your production/host environment. It looks like you are on windows but you will probably deploy to a linux os. In windows the directory separator is \ in Linux /.

C:\wamp\www\PracticePhP\templates\PHPPRA\STILL/…/…/form.html.php

Does that look like a valid file path mixing \ and /.

Because you append a relative path onto the end, if you place the file into a different directory level then it will fail.

As I understand it, ../ signifies “go up a level”, so you’re saying here “go up two levels from the directory the running script is in”. That’s great, unless you move your script to somewhere a level further up, or down, and the relative path doesn’t work out any more.

I’ve always wondered about that, but a note that I saw just now suggests that as long as you stick to using “/” everything should be OK.

You can also use the DIRECTORY_SEPARATOR constant.

See http://php.net/manual/en/dir.constants.php

1 Like

Hello,
Thanks for answering me.

I made sure that I use “/” as it is in windows.

Yet, when I move the including file to a different directory - it doesn’t work.
I guess that changing the original path “breaks” a “link”.
So what is the puppose of this "magic " constant?

Hello,
I made sure I’m using “/” all along as I use windows.
Yet when I move the including file to a different directory, It won’t work and I get the same error messages.
So what is the purpose of this "magic " constant?

Are you moving it to a different directory at the same “level” as the old one, or is it higher or lower in the directory tree? As I said earlier, your link moves “up” two levels in the directory tree to find the file to include, so if you’ve now moved the file higher or lower, it won’t end up in the desired location.

It’s better to use this than to hard-code a directory path, in case you move the script around for some reason. But you always need to be careful when using relative paths, whether you use the built-in variable or hard-code the link.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.