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.
Related
Okay so I don't know if this is a "can't see the forest for the trees" situation but I'm slowly losing my patience on this.
All I want to do is find a way to include files from the project's root, no matter how nested the file, no matter where it's all hosted.
I obviously can't just do absolute paths like "/includes/file.php".
I can't use $_SERVER['DOCUMENT_ROOT'] because, as far as the servers where I'm hosting it are concerned, that's the wrong path.
I read about creating a config.php file in the root directory and defining the ROOT_DIR there, but that means I have to include that file RELATIVE in every script, which makes things unstable again. (I don't want every include to break because I moved the file somewhere else)
I can't use constants like DIR because it returns the current directory, not the absolute project root.
I tried to set the include path, which doesn't seem to work.
The only "solution" that works for me as of right now: In every file, I define a $root variable and set it to the path that I know is the correct project root.
That can't possibly be the best solution, can it?
There must be an easier way. And the fact that I can't find it drives me crazy.
The root path that works looks something like this:
"/mnt/web10145/dd3/519/5878104319/htdocs/project" (numbers changed)
Using this for includes works just fine. However, calling set_include_path($path) and then including without that path doesn't work.
Please, if anyone knows anything: Every answer is deeply appreciated.
Don't mind pointing out how stupid I am to not have thought about xyz. If it helps me solve the problem it's all good.
Thank you!
Although I have a fair idea of relative path but I am a little confused while using this particular relative path in a program. Please someone guide me through. I have the following directory structure:
The problem here is that I want to include config.php file in left.php, for that I have given a relative path, include(../../layout/config.php); but somehow this is not being included and the relative path that I have to use is include(../layout/config.php);. Can someone please explain why is that so? Because my knowledge says that I will have to leave includes->admin and then enter layout.
In PHP it's the working direcory that is used as the main path.
All relative paths originate from that folder.
As you wrote in comments you use index.php which is one folder lower than left.
I assume you are in index and include left.php, this does not update the working directory.
Your working directory is still "admin".
You can find out what the working directory is using echo getcwd();
http://php.net/manual/en/function.getcwd.php
As I wrote in comments you can use include($_SERVER['DOCUMENT_ROOT'] . "/Electronix_Store/electronix/layout/config.php"); to use full path instead.
This is easier if the files and folders are in the same position all the time.
My advise is to only use relative paths if you are working with day "user/123/" -> some folder structure in there.
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.
I wrote this code in a test.php file.
<?php
include ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/autoload.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Client.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Service/YouTube.php');
?>
If I go to this file like this: localhost/MySite/protected/MyYouTube/test.php it works, but if I copy the same code into my controller that is located under the same folder as test.php, I get this:
include(Google_Service.php): failed to open stream: No such file or
directory
There are no conflicts with the imports. In fact, the controller and view can be empty and still I get the same thing.
Apparently that happens when autoload.php is not actually loaded.
How is it possible that when the code is integrated into the website it throws this error?
This is what the path to my site looks like:
localhost/MySite/index.php/user/view It seems that the way I visit the file matters.
I tried several things. I tried importing the test.php into my view or my controller, and still I get the same error. I tried using Yii::app()->basePath and it gives the same problem.
The paths are correct, I have checked several times. How can I fix this?
The include path is actual server path, not site path, if you use / at the beginning, you are telling it to look in the server root.
If you know the absolute path in the server you can use it like /var/www/MySite or c:\mysiteif you don't know that then you use relative paths.
$_SERVER["DOCUMENT_ROOT"] works well with PHP 5.3, not sure if below check your version. Try removing the '/' before the MySite part, as the variable already does it for you so it might be printing like localhost//MySite, although I'm not sure if that should or shouldn't work. ]
Also the autoload php should be loaded with a require_once function, not with the include. Good luck!
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'