PHP Require Path - php

I have a directory layout something like /public_html/foo/bar.php that I want required in my file with my file require statement being currently require('../foo/bar.php') but I'm not getting to it (I keep getting an error saying its not a directory? and failed to open stream).
My file that I'm putting the require in is in /public_html/foo2/ so I need to go up one directory listing and then find the file to include it but I'm not sure what I'm doing wrong right now. Any guesses?
Actual code:
require('../objects/user.php/');
Thanks for your help!

You have a / behind your ../objects/user.php suggesting user.php is a directory.
That's why it tells you it is not a directory, because it indeed isn't.
Remove it and you'll probably be fine. (so ../objects/user.php/ becomes ../objects/user.php)

Related

PHP Fake require_once to always return true

Is it possible to "fake" the inclusion of a file ?
I am testing a code base where DI hasn't been implemented and want to include a class file with my own version before it gets required. When it hits the actual require line, I get complaints about the class already being added.
So when I do require_once on the file, I don't want to actually include it, but it to carry on like it did.
You can use include_once() which won't give you breaking errors but will still try and include the file.
My solution was to duplicate the project and change directory before including the configuration files containing require statements: except in the copied directory, the files were empty.
Then I switched back to the current directory.

Error inserting a .php file using the require directive

I am pretty new in PHP and I have the following problem trying to define a required file into a .php file, I have done in this way:
require("../../common/define.php");
because the define.php is into the common/ two level back related to the file in wich I am instering this require directive.
But this syntax seems to be wrong because it give me this error message:
Warning: require(../../common/define.php): failed to open stream: No such file or directory in C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php on line 3
Why? What is wrong? How can I insert a required file that is some level back in the tree?
PHP current file:
C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php
So then in this file you have a request for ../../common/define.php
Which is two steps [folders] back, then into the common folder so;
This is two steps back:
C:\xampp\htdocs\PandaOk\templates\
And into the common folder:
C:\xampp\htdocs\PandaOk\templates\common\
So what you've given is a directory that doesn't exist, which is exactly what the error tells you.
instead, only go one step back:
require("../common/define.php");
Alternatively, and far better practise, is to do an absolute file path using $_SERVER['DOCUMENT_ROOT'] such as something like:
require $_SERVER['DOCUMENT_ROOT']."/templates/default/common/remove-booking_pc.php";
This would mean that the reqire always succeeds regardless of where in your project diretory tree it is called from.
(I don't know what directory your document_root will be defined as, but you should get the idea, I hope)
You can also give a full static file path such as commented by Sahil Gulati.
require "C:\xampp\htdocs\PandaOk\templates\default\common\define.php";
P.S: Require and include do not need brackets.

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 - Going back directories

I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.

PHP is looking for a file in the wrong place

I am an amateur web developer and I am trying to get my site live for the first time. The site is working and all of the files are uploaded but the site is not loading my PHP includes.
Here is the error message:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home4/myUsername/public_html/index.php on line 3
How can I get PHP to look in public_html/ rather than public_html/index.php?
EDIT: I have tried editing the include path. It doesn't seem to change where php is looking for the file. Additionally my includes work properly in localhost.
I'm going to assume this is your folder structure:
public_html/index.php
public_html/includes/header.php
Generally (not always), $_SERVER['DOCUMENT_ROOT'] will now reflect the path to the base public_html directory (this I'm assuming based on the context of your message). This means you can always point to the root this way. - no matter if you have /index.php or /my/deep/file/structure.php
Try this with your include statement on index.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
You may need to change the include path in your php.ini file or use set_include_path() to change the include path.
Here is the manual entry for the function call if you'd like to read more about it.
Have you checked already the include file?
in given. include(folder_includes/file_header.php);

Categories