My home directory path is /home9/ but when I using the __DIR__ code to get the path, I get /home/ it's missing the number 9, I know I can get the path by using $_SERVER['DOCUMENT_ROOT'], I just want to know why and how to fix it, I really need to use the __DIR__ code.
<?php
// this is the first code
echo __DIR__ .'<br>'; //I get /home/username/public_html/ is the wrog path
// this the secound code
echo $_SERVER['DOCUMENT_ROOT']; //I get /home9/username/public_html/ the correct path
?>
I contacted my hosting provider and they couldn't help, please help.
These are 2 different things.
Here in your case /home9/username/public_html/ is symlinked to /home/username/public_html/ where /home9/username/public_html/ is the actual document root. However __DIR__ returns the real path where symlink resolves to.
You can refer php documentation for more information in between these 2.
Related
I've followed different guides in uploading my Laravel project in Hostinger, but unfortunately there is always an error, it won't load the index.php.
Here is the structure in my file manager.
https://imgur.com/a/3lLX9d3
This is my public files in the public_html
https://imgur.com/a/3xkf7PL
This is the code inside index.php
require __DIR__.'/../sample/vendor/autoload.php';
$app = require_once __DIR__.'/../sample/bootstrap/app.php';
When I try to open the site.
https://imgur.com/a/c1odO4A
Did I miss something?
For some more clarification on the issue, I decided to post an answer.
The main problem that provided code has is
require __DIR__.'/../sample/vendor/autoload.php';
$app = require_once __DIR__.'/../sample/bootstrap/app.php';
Both paths are probably invalid. Judging by earlier comments, the foders don't exist.
According to Manual _DIR_ will hold the directory of a current file - actually more correct would be sayin it holds a directory of a file in which it resides.
_DIR_ variable is hard to work with, when it comes to creating absolute paths, because it already is almost an absolute path of a current file.
There are several other ways to get the correct path, and among them the most basic, probably easiest and probably fastest would be using $_SERVER['DOCUMENT_ROOT'], which holds
The document root directory under which the current script is executing, as defined in the server's configuration file. From Php Manual
I will try to provide some exaples which show the issues with _DIR_ variable and how $_SERVER['DOCUMENT_ROOT'] can be used.
I will also be somewhat replicating your structure at a very basic level.
NOTE: All my examples are running on a localhost under Windows, which only further will prove that _DIR_ is un-reliable
I took your file structire and simlified it to just two folders, one file in each folder. My relative file structure looks literally like:
public_html/index.php and sample/include.php.
Now inside index.php first you put this code
<?php
echo __DIR__.'/../sample/include.php<br>';
?>
Which will output C:\xampp\htdocs\myWordpress\public_html/../sample/include.php when I run it. The output is a valid path on Windows though and doing require on it should work.
Now I am changing index.php to
<?php
echo __DIR__.'/sample/include.php<br>';
?>
NOTE: i basically only removed /../ from the path.
When i run it again - the output will be C:\xampp\htdocs\myWordpress\public_html/sample/include.php which will be a very invalid path on Windows. - folder does not exist.
The two examples above proove that _DIR_ variable is un-reliable.
Now consider the following index.php
<?php
echo $_SERVER['DOCUMENT_ROOT'].'/sample/include.php<br>';
?>
When I run this - it will output `For some more clarification on the issue, I decided to post an answer.
The main problem that provided code has is
require __DIR__.'/../sample/vendor/autoload.php';
$app = require_once __DIR__.'/../sample/bootstrap/app.php';
Both paths are probably invalid paths, since both probably do not exist.
According to Manual _DIR_ will hold the directory of a current file - actually more correct would be sayin it holds a directory of a file in which it resides.
_DIR_ variable is hard to work with, when it comes to creating absolute paths, because it already is almost an absolute path.
There are several other ways to get the correct path, and among them the most basic, probably easiest and probably fastest would be using $_SERVER['DOCUMENT_ROOT'], which holds
The document root directory under which the current script is executing, as defined in the server's configuration file. From Php Manual
I will try to provide some exaples which show the issues with _DIR_ variable and how $_SERVER['DOCUMENT_ROOT'] can be used.
I will also be somewhat replicating your structure at a very basic level.
NOTE: All my examples are running on a localhost under Windows
I took your file structire and simlified it to just two folders, one file in each folder. My relative file structure looks literally like:
public_html/index.php and sample/include.php.
Now inside index.php first you put this code
<?php
echo __DIR__.'/../sample/include.php<br>';
?>
Which will output C:\xampp\htdocs\myWordpress\public_html/../sample/include.php when I run it. The output however is valid path on Windows and doing require on it should work.
Now I am changing index.php to
<?php
echo __DIR__.'/sample/include.php<br>';
?>
NOTE: i basicallyc only removed /../ from the path.
When i run it again - the output will be C:\xampp\htdocs\myWordpress\public_html/sample/include.php which is a very invalid path on Windows. The folder does not exist.
Now consider the following index.php
<?php
echo $_SERVER['DOCUMENT_ROOT'].'/sample/include.php<br>';
?>
When I run this - it will output C:/xampp/htdocs/myWordpress/sample/include.php which is an Existing, Valid Path, that can be later used in require for example.
A working example that shows the usage and the output differense between _DIR_ and $_SERVER['DOCUMENT_ROOT'] will probabply look like below
index.php
<?php
echo 'Directory of index.php file is:<span style="color:red">'. __DIR__.'</span></br>';
echo 'File include.php will be included from:<span style="color:red">'. $_SERVER['DOCUMENT_ROOT'].'/sample/include.php</span></br>' ;
require_once $_SERVER['DOCUMENT_ROOT']."/sample/include.php";
?>
include.php
<?php
echo 'Directory of include.php file determined by `__DIR__` is:<span style="color:red">'. __DIR__.'</span></br>';
echo 'Directory of include.php file determined by usage of $_SERVER[\'DOCUMENT_ROOT\']:<span style="color:red">'. $_SERVER['DOCUMENT_ROOT'].'/sample/include.php</span></br>';
?>
I made the paths red, to bring extra attention to them.
This example will show that __DIR__ and $_SERVER['DOCUMENT_ROOT'] return different results, on the same system, on the same virtual server.
DIR can also be used to achive the same result with pure Windows paths - it will be ugly though.
Adding line
echo 'The valid directory of include.php file using __DIR__ is:<span style="color:red">'.__DIR__.'\\..\\sample\\</span></br>';
to index.php will somewhat show that DIR can be used on Windows, and Windows only!, on Unix this should and would fail. It also shows that it's usage is somewhat platform specific, which also means not very reliable.
EDIT
The example above is actually very bad. It shows wrong and ugly coding. And I would not have shown it, if I have't seen an actual production code like this once. (Not mine)
The bottom line for all these examples is - Use $_SERVER['DOCUMENT_ROOT'] in creating paths and do not use __DIR__.
I hope my examples somewhat show why i think $_SERVER['DOCUMENT_ROOT'] is a better idea.
Edit 1 I guess my links to doc's and explanation were not that good.
Judging by the images of your file structure your paths should be:
require $_SERVER['DOCUMENT_ROOT'].'/sample/vendor/autoload.php';
$app = require_once $_SERVER['DOCUMENT_ROOT'].'/sample/bootstrap/app.php';
But that really depends on what $_SERVER['DOCUMENT_ROOT'] contains. And what the relative paths to $_SERVER['DOCUMENT_ROOT'] contain.
Suppose i have this file:
/Home/user/docs/somewhere/inHere.php
And in this php, i want to require this:
/Home/user/other/well/buried/place.php
I know the difference between an absolute and relative path, but cannot seem to figure out how php wants this to look, i keep getting 'file not found or does not exist'
I am on a hostgator shared web server, if that has any bearing on anything.
Include it using absolute paths.
Either:
include '/Home/user/other/well/buried/place.php';
or do it relative from where you are, but still absolute:
include __DIR__ . '/../../other/well/buried/place.php';
The magic constant __DIR__ contains the absolute path to the file it was written in.
If you just do a relative path include '../../and-so-on', the starting point will change if you're including that file in some other file that resides in some other location.
Firstly you can get the current folder using getcwd().
Next, you can use $path = '../../etc'; $realPath = realpath($path). It will return false if the path is wrong, and the concrete path without relative ../'s if it is indeed an actual path.
If you still can't get it, var_dump($path); and then copy the path and try to cd into it, you should diascover what you are doing wrong at that point.
From the file location (somewhere) you have to go upwards (to docs) and another time (to user), and then go inside "other", like this:
include ("../../other/well/buried/place.php");
There you go.
I have Joomla Component administrator/components/com_mycomp
And I have import.php file in this folder.
How I can access import.php from com_mycomp.php.
require_once('./import.php');
Gives error file not found. Because current path is /administrator , instead of administrator/components/com_mycomp.
The easiest way is to use PHP's __DIR__ constant, that always points to the current file's directory.
<?php
require_once __DIR__ . '/import.php';
use JPATH_BASE;
<?php
require_once(JPATH_BASE.'/directory/subdirectory/import.php');
?>
https://docs.joomla.org/How_to_find_your_absolute_path
Best way is to use Joomla constants, use
JPATH_ADMINISTRATOR - The path to the administrator folder.
or
JPATH_COMPONENT_ADMINISTRATOR - The path to the administration folder of the current component being executed.
for more refer-
https://docs.joomla.org/Constants
Let me know if still face any issue.
I've been going over those two topics:
include, require and relative paths
PHP - with require_once/include/require, the path is relative to what?
and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong.
Anyway this is where my problem occurred:
Root/ //this is root location for server
APP/ //this is root location for script
Root/APP/core/init.php //this is where I include classes and functions from
Root/APP/classes/some_class.php //this is where all classes are
Root/APP/functions/some_function.php //this is where all functions are
and so obviously I need to include init.php everywhere so I did in every file like this:
require_once 'core/init.php';
it was working until I have decided to create a location for admin files like this:
Root/APP/Admin/some_admin_file.php
and when I included init this way:
require_once '../core/init.php';
script failed to open functions, no such file in APP/Core/ folder
so I used DIR method presented in topic above and than even weirder thing happened, error:
no such file in APP/Core/classes/Admin/
What is that? :D I'm lost with this, could someone help a bit ;)
Include paths are relative to the current working directory, which can be inspected using getcwd(); this can be a source of many issues when your project becomes bigger.
To make include paths more stable, you should use the __DIR__ and __FILE__ magic constants; for instance, in your particular case:
require_once dirname(__DIR__) . '/core/init.php';
The dirname(__DIR__) expression is effectively the parent directory of the script that's currently being run.
Btw, __DIR__ could also be written as dirname(__FILE__).
I have seen this:
<?php
include( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my_file.php');
?>
Why would I ever need to do this? Why would I go to the trouble of getting the dirname and then concatenating that with a directory separator, and a new filename?
Is the code above not equivalent to this:
<?php
include( 'my_file.php' );
?>
??
The PHP doc says,
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing. The include() construct will emit a warning if it cannot find a file; this is different behavior from require(), which will emit a fatal error.
Let's say I have a (fake) directory structure like:
.../root/
/app
bootstrap.php
/scripts
something/
somescript.php
/public
index.php
Now assume that bootstrap.php has some code included for setting up database connections or some other kind of boostrapping stuff.
Assume you want to include a file in boostrap.php's folder called init.php. Now, to avoid scanning the entire include path with include 'init.php', you could use include './init.php'.
There's a problem though. That ./ will be relative to the script that included bootstrap.php, not bootstrap.php. (Technically speaking, it will be relative to the working directory.)
dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap.php resides.
(Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__).)
Now, why not just use include 'init.php';?
As odd as it is at first though, . is not guaranteed to be in the include path. Sometimes to avoid useless stat()'s people remove it from the include path when they are rarely include files in the same directory (why search the current directory when you know includes are never going to be there?).
Note: About half of this answer is address in a rather old post: What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?
I might have even a simpler explanation to this question compared to the accepted answer so I'm going to give it a go: Assume this is the structure of the files and directories of a project:
Project root directory:
file1.php
file3.php
dir1/
file2.php
(dir1 is a directory and file2.php is inside it)
And this is the content of each of the three files above:
//file1.php:
<?php include "dir1/file2.php"
//file2.php:
<?php include "../file3.php"
//file3.php:
<?php echo "Hello, Test!";
Now run file1.php and try to guess what should happen. You might expect to see "Hello, Test!", however, it won't be shown! What you'll get instead will be an error indicating that the file you have requested(file3.php) does not exist!
The reason is that, inside file1.php when you include file2.php, the content of it is getting copied and then pasted back directly into file1.php which is inside the root directory, thus this part "../file3.php" runs from the root directory and thus goes one directory up the root! (and obviously it won't find the file3.php).
Now, what should we do ?!
Relative paths of course have the problem above, so we have to use absolute paths. However, absolute paths have also one problem. If you (for example) copy the root folder (containing your whole project) and paste it in anywhere else on your computer, the paths will be invalid from that point on! And that'll be a REAL MESS!
So we kind of need paths that are both absolute and dynamic(Each file dynamically finds the absolute path of itself wherever we place it)!
The way we do that is by getting help from PHP, and dirname() is the function to go for, which gives the absolute path to the directory in which a file exists in. And each file name could also be easily accessed using the __FILE__ constant. So dirname(__FILE__) would easily give you the absolute (while dynamic!) path to the file we're typing in the above code. Now move your whole project to a new place, or even a new system, and tada! it works!
So now if we turn the project above to this:
//file1.php:
<?php include(dirname(__FILE__)."/dir1/file2.php");
//file2.php:
<?php include(dirname(__FILE__)."/../file3.php");
//file3.php:
<?php echo "Hello, Test!";
if you run it, you'll see the almighty Hello, Test!! (hopefully, if you've not done anything else wrong).
It's also worth mentioning that from PHP5, a nicer way(with regards to readability and preventing eye boilage!) has been provided by PHP as well which is the constant __DIR__ which does exactly the same thing as dirname(__FILE__)!
Hope that helps.
I used this below if this is what you are thinking. It it worked well for me.
<?php
include $_SERVER['DOCUMENT_ROOT']."/head_lib.php";
?>
What I was trying to do was pulla file called /head_lib.php from the root folder. It would not pull anything to build the webpage. The header, footer and other key features in sub directories would never show up. Until I did above it worked like a champ.
If you want code is running on multiple servers with different environments,then we have need
to use dirname(FILE) in an include or include_once statement.
reason is follows.
1. Do not give absolute path to include files on your server.
2. Dynamically calculate the full path like absolute path.
Use a combination of dirname(FILE) and subsequent calls to itself until you reach to the home of your '/myfile.php'.
Then attach this variable that contains the path to your included files.