Problem requiring resource PHP Codeigniter - php

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'

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.

Root of website in a subdirectory in 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.

How come my code works separately but not when I merge it with my controller?

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!

php include vs library; correct path

I have been trying to research what is better to use in order to include repetitive parts of the website - php include function or library(provided in Dreamweaver).
Or maybe there are other - better ways to achieve the same result?
At the moment I use php include and absolute paths. I downloaded the website from the server but it seems that the paths that work on the server don't work on my localhost.
What would be the correct and the best way to write paths in order to make them work on both servers without having to re-write the code?
Thanks
You can call at the start of your script the set_include_path() command and specify relative paths to your libraries
http://www.php.net/set_include_path
Since dream weaver uses library, that's probably wrong ;)
If you want to be sure the code is only included once you might use require_once instead.
Try relative paths instead of absolute
Also an easy way to solve this problem is to define a constant from within the root, normally within your index.php file.
/**Path Environment**/
$root=pathinfo($_SERVER['SCRIPT_FILENAME']);
define ('BASE_FOLDER', basename($root['dirname']));
define ('SITE_ROOT', realpath(dirname(__FILE__)));
define ('SITE_URL', 'http://'.$_SERVER['HTTP_HOST'].'/'.BASE_FOLDER);
Then you can use, SITE_URL and SITE_ROOT throughout your script knowing its the correct path to your web root.
So your old includes look like:
include('/srv/www/somesite.com/public_html/somefile.php');
and your new includes will look like include(SITE_ROOT.'/somefile.php'); then when developing on windows the path will change accordingly.
Put
include_path = ".:/your/absolute/path"
in your php.ini, both Server environment and localhost.
Then you can access the include path file at anywhere of the codes.

use absolute or relative path?

in my config.php where i have all constants i set the PATH to a absolute path.
but this means that when i move my application folder i have to change this path.
i wondered if its better to set a relative path, in that way whenever i move my application between production and development folder, i dont have to change it.
how do you guys do when you move between folders?
The best way I've found is to do the following:
define("PATH", realpath(dirname(__FILE__)));
That gives you the directory of the current file. If you do this in your settings/bootstrap/init file, you'll have it available to your application, and it will work for any file system.
__FILE__ is your friend.
define('BASE_PATH', dirname(realpath(__FILE__)));
This will make your scripts more portable.
Include a file like this
include BASE_PATH . 'includes/header.php';
IMO, absolute paths are bad news. Even if you don't plan to move, your hosting provider could move you, like DreamHost recently did to me. I was fine....
But there are 14 references to "path" on their wiki:
http://wiki.dreamhost.com/Server_Moves
I do three things to solve this:
The first is to use paths relative to the current file and include things using dirname(__FILE__).
The second is to use a loader include that all the pages load. This file has one responsibility: to find the include directory, usually via a relative call. So long as this relative relationship stays, it doesn't need changing.
I also like to support custom settings that belong to the installation rather than the codebase. This is done by an include mechanism and overrides a few settings that will be specific for the server the code is on.

Categories