I have a variable on my site called $basePath which is set as:
$basePath = '/Systems/dgw/';
I am using it on all my css, js and images tags as so (shortened for better visibility):
<link href="<?php echo $basePath; ?>include/assets/css/bootstrap.min.css">
I have no problem with those includes and they work fine in wherever file and in whatever folder I am.
I have a certain included page which has the following line:
<img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/>
And the image shows just fine. The line after it states:
<?php include($basePath.'include/assets/common/topMessages.php');?>
But the include doesn't happens. When I try it like this:
<?php include('../../include/assets/common/topMessages.php');?>
It works.
Anybody has any idea what could be wrong?
If you mean the path where the image was located on the user computer before he/she uploaded it to your form - you can never know it in php or javascript or anywhere else. In PHP you can see the path on SERVER (usually in the temporary folder) where the file was stored so you can read or copy it.
To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.
Relative pathsIf you don't supply the root, it means that your path is relative. The simplest example of relative path is just a file name, like index. html . So one should be careful with relative paths. If your current directory is /about/ then index.
The way I do it is visual. I put my mouse pointer on the index. php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.
You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :
<?php $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/yourpath/yourfile.php"; include_once($path); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With