Root of website in a subdirectory in PHP - php

I'm developing a website in PHP.
The test environment is in a subdirectory of the root and there are multiple test environments. But the website doesn't work properly because of al the absolute and also the relative paths.
So i need a clean solution to get the path of the root of the website and not of the server.
I've tried a lot of thinks but nothing seems to work.
I hope you have a beter solution for this problem.
Thanks in advance!

Not sure if this will help, but if you want to get directory of current php file you should use __DIR__ constant. More info here.
If your website index is located at /home/www/subpath/index.php, this code in index.php:
echo __DIR__;
will return:
/var/www/subpath

You can use a SESSION to set your desired PATH and use it on all the path you need.
If you are using a framework like Laravel this can be done a lot easier.

Related

Relative path works but Absolute path doesn't work

I am a newbie in php and I have a little struggle with making a project work .
my project folders are displayed like this:
-> Project
->doc1
->web
*page1.php
->menu
*menu.php
I tried accessing menu.php in my file page1.php using relative path like this:
#require "menu/menu.php";
And it works. But when I try to use absolute path like this:
#require "/project/web/menu/menu.php";
it doesn't work for me. I don't know where the problem exactly comes from. I hope someone can guide me and show me where I missed up or what do I need to verify to fix the problem.
P.S: the reason I wanted to use absolute path because the project get messed up moving from a directory to another, so I want to make it all absolute path so I don't get confused since I am just starting using php.

dirname(__FILE__) adds path after my domain

Here's my problem using the PHP function dirname()
Let me first explain, I'm working on a website where I defined a constant as follow :
define('ROOT', dirname(__FILE__));
That worked well on a shared hosting, but I had to move the website on a dedicated server and now some files I try to call with an absolute link return 404. When I look what path they use, something like this appears :
http://myIp/var/www/myWebsite/[...]/image.jpg
What happens is that, my 'ROOT' constant is '/var/www/myWebsite' but in place of 'Replacing' my domain, it's added at the end of it. Which makes no sense since my Apache VirtualHost sends all requests towards 'myIp' to the local folder '/var/www/myWebsite'.
I don't know if the mistake is either from my PHP code, or my Apache VirtualHost.
Thank you for your help, I'm sure it's something stupid but I can't figure what I did wrong :)
I think you are confusing server paths with site paths.
You would not want to use dirname to give you a path for a web asset.
What you could do if you really want to do it this way is to remove the path to your web root.
This might work:
define('ROOT', str_replace('/var/www/myWebsite/', '/', dirname(__FILE__)));

Base Url PHP includes on WAMP

I am building a PHP mySQL site locally with WAMP.
in my WAMP www directory i have made subfolders for each website I am making. eg:
WAMP\www\site1\index.php
WAMP\www\site2\index.php
WAMP\www\site3\index.php
etc
my DB connection is stored in:
site1/connections/open.php
my PHP scripts are stored under:
site1/php/filename.php
when i am running a script i need to include the open.php connection. the only way i can get this to work is by using:
include '../connections/open.php'; // this goes at the top of the PHP script
I know that if i use this and then move to remote servers or move some directories around I will have problems accessing this file. I therefore want the path to be relative to the basedir or baseurl(/site1/).
I understand that when you use an include it is looking for a dir and not an url so I cant use anything like;
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
How do you go about sorting this out? Thanks
Yes you are right on that URL part
I understand that when you use an include it is looking for a dir and
not an url so I cant use anything like;
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
How do you go about sorting this out? Thanks
You should try like this
$_SERVER["DOCUMENT_ROOT"]."/myFolder/"
Or try this
getcwd()."/myFolder/"
But there is a difference between both of them [Try Googling]
I think when you move to remote servers or move some directories around you will have no problems,because you are using relative path.
I think you should not use "$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']" and I think you should know what they mean!

DOCUMENT_ROOT is not complete, missing domain folder

I have searched many threads so far but cant seem to find a solution. Inside one of my php scripts I am trying to get a server document root but the value I get is not complete, its simply missing the domain folder. I believe it is due to sharing hosting or smth else.
Here is the current way I am using:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
and the path I get is like:
/home/content/01/0151247/html
although I know it should be like:
/home/content/01/0151247/html/mydomain
I know as I compared it with SCRIPT_NAME and I see the mydomain there in the path.
Hope someone could direct me.
Thank you and sorry for probably asking another thousand time same question over community, I really tried things around from here, nothing helps me so far.
UPDATE
unfortunately I cant not simply use my index file with DIR as it is a wordpress setup and I am working on a separate folder where I am including some wordpress functionality but for that I need a document_root. If that would help.
UPDATE
apparantly the following way resolved my case, maybe it will help someone one day:
realpath($_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]);
basically because of the server setup and domain configured as a subdomain.
Thanks to all who participated.
Prior to PHP 5.3 you can put a file in the directory whose path you want and define a constant:
define('ROOT_DIR', dirname( __FILE__ ));
After 5.3 you can just do:
define('ROOT_DIR', __DIR__);
The idea being that this would be in config.php of some sort that is included every time the application runs.
Magic Constants Docs
UPDATE
In the config file, you can just append the DOCUMENT_ROOT variable:
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/mydomain';
And that should take care of it for you.
Old Solution
The DOCUMENT_ROOT is an environment variable set by the server. So if this is on shared hosting, you cannot changed. An alternative is to set your own constant to it, so in a config type file that is included on your pages you can do something like:
define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/mydomain');
And then just use that constant in place of $_SERVER['DOCUMENT_ROOT']. The other option is to contact your host and inquire about it, maybe it was an oversight on their part and they will fix it.
EDIT
Probably using the __DIR__ as others have posted about is the better way, as the DOCUMENT_ROOT can be set to different items and at least with the __DIR__ you should get an accurate directory each time.
Personally, to get the root of a folder in PHP, I use this in the my index file:
define('ROOT', dirname(__FILE__)); // __DIR__ will work under PHP 5.3

Problem requiring resource PHP Codeigniter

So I had an issue last night when I went to upload a project I've been working on locally to my server.
I had this:
require_once "/../controllers/source/MySpaceID/myspace.php";
Which is the correct path to that file finding its way out of the libraries folder. It worked fine until I put it on the server. Any thoughts?
Echo you current working directory on the server ( echo getcwd(); ) and check your path from there, that's probably where your problem lies.
It sounds strange to me that CI would use ./library as its working directory - unless you set it yourself.
Note that you should use code igniter's APPPATH constant to create absolute paths instead of using relative ones, this will make things easier for you on the long term.
Lepidosteus is correct. Your library should live in /application/library/ and you should require it like this require_once APPPATH.'/libraries/MySpaceID/myspace.php'

Categories