Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include absolute path

Tags:

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?

like image 644
digicom Avatar asked Apr 07 '14 12:04

digicom


People also ask

How can I get full image path in PHP?

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.

How do you create an absolute path?

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.

What is relative path in PHP?

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.

HOW include PHP file in another directory?

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.


1 Answers

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); ?> 
like image 56
Laurent S. Avatar answered Sep 16 '22 21:09

Laurent S.