Usage of __DIR__ constant

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
erez
New php-forum User
New php-forum User
Posts: 1
Joined: Sat May 19, 2018 1:16 am

Sat May 19, 2018 6:25 am

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

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

Code: Select all

<?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

Code: Select all

<!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

Code: Select all

<!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 do I take advantage of the __DIR__ constant if It fails when I move the including file to a new directory?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 708
Joined: Mon Feb 22, 2016 5:52 pm

Sat May 19, 2018 7:24 am

If I understand correctly, the __DIR__ "knows" where the including file is located
__DIR__ knows which directory the currently executing file is located.
so that if I move the file to a new directory it should work without updating the code.
No, you need to change your include statement, the server can only find files when it knows where to find them; __DIR__ will be set to the directory of the currently executing file: 'main.php'

I use:

Code: Select all

$root=$_SERVER['DOCUMENT_ROOT'];
# and then to include the file:
include $root.'/path/form.html.php';


But you still need to update the path when you change the location of the file you want to include, using ../ can really give you headaches and can get you into trouble if you're not very careful.

Post Reply