Getting Relative Paths - php

Super noob question here...
The file source:
www.mysite.com/blah/blah/customdir/ajax/folder/file.php
and I want to include a source:
www.mysite.com/blah/blah/customdir/include/func/functions.php
So I have tried:
require_once( '.../include/func/functions.php' );
require_once( '../include/func/functions.php' );
require_once( '././include/func/functions.php' );
and a ton of other variations. How do I get to it? Setting a top level path on every file isn't an option (that's what the function file is for...)
Each of the above is giving me an error:
Warning: include(././include/func/functions.php) [function.include]: failed to open stream: No such file or directory in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3
Warning: include(././include/func/functions.php) [function.include]: failed to open stream: No such file or directory in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3
Warning: include() [function.include]: Failed opening '././include/func/functions.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mysite/public_html/mysite.com/customdir/ajax/folder/index.php on line 3

There are many ways to include a file. However, it seems you are attempting relative paths. In which case, try:
require_once '../../include/func/functions.php';
I encourage you to learn more about your options for including files. But in a nutshell, each ../ takes you back a directory. ./ represents the current directory. Otherwise, you can always use an absolute path or even (gulp) a hybrid.

Are you running under apache? use getenv("DOCUMENT_ROOT") and set your path from there.

Related

Include cant find directory PHP

i make MVC application (Without frameworks).
I try to include models from folder 'models' in controller, in folder controller.
include("/models/tasks_model.php");
But i get error:
Warning: include(): Failed opening '/models/tasks_model.php' for inclusion (include_path='.') in ...controller\create_task_controller.php on line 3
And
Warning: include(/models/tasks_model.php): failed to open stream: No such file or directory in ...\controller\create_task_controller.php on line 3
You need to fix your PHP paths
include("../models/tasks_model.php");
When you use this code:
include("/models/tasks_model.php");
It looks from the start of your disk because it an absolute path.
../ Goes back one folder from where your file is.

Include php file

I have the following directory tree on a linux server:
/home/control-center/back-office/php/average.php
/home/control-center/front-office/db.php
db.php contains the database connection information.
When I try to execute average.php, I get the following error message:
PHP Warning: include(../../front-office/db.php): failed to open stream: No such file or directory in /home/control-center/back-office/php/average.php on line 18
PHP Warning: include(): Failed opening '../../front-office/db.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/control-center/back-office/php/average.php on line 18
Should I include something else in my script so it would work? I found a couple of similar cases on the Internet, but I haven't managed to make it work.
Try include dirname(__FILE__) . '/../../front-office/db.php';
dirname(__FILE__) returns the absolute path of the current file. This ensures that you get the correct path relative to the current file.
Your problem is often encountered when the current script was included by another script. You can check this answer which is related to what I mentioned.
How do you include these 2 pages in your avarage.php file?
if you want to include header.php in index.php page which is in "sample" folder (sample/index.php) and header.php in include/header.php, then you have to write "../" before including a directory, so that would be like:
include '../include/header.php';

Warning: require() [function.require]: failed to open stream: No such file or directory in

This error is quite common and it generally means it can't find the file it's looking for, but I can see the file in the directory?!
Full error:
Warning: require(/home/coinsfut/public_html/files/core.php) [function.require]: failed to open stream: No such file or directory in /home/coinsfut/public_html/new/index.php on line 4
line 4 says:
require $_SERVER['DOCUMENT_ROOT']. "/files/core.php";
the site is hosting in /new
any ideas?
Check the warning again. You are trying to include
/home/coinsfut/public_html/files/core.php
when in fact you want
/home/coinsfut/public_html/new/files/core.php
Please try
require $_SERVER['DOCUMENT_ROOT']. "/new/files/core.php";
and it should work just fine.
Alternative you could use
require "files/core.php";
to include files in a relative sub-directory since your index.php is in "new" directory.

Fail to use include without getting an error:

I am trying to use this:
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Warning: include_once(../../untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'../../untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
and this:
include_once("/PoliticalForum/untitled/sanitize_string.php");
Warning: include_once(/PoliticalForum/untitled/sanitize_string.php) [function.include-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php on line 3
Warning: include_once() [function.include]: Failed opening
'/PoliticalForum/untitled/sanitize_string.php' for inclusion
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\number_login_attempts.php
on line 3
How can I import files safely without having those errors?
Pick one:
Provide a valid path (paths that start with / are absolute, otherwise they are relative).
Add the folder to the include_path.
Use class autoloading (if applicable).
You should make sure the files can be found on the include path, because these errors mean the file cannot be found by PHP.
More info about include can be found in the PHP manual: http://php.net/manual/en/function.include.php
I think those files you include each inlude other files (possibly with require or require_once). These files cannot be found which causes the include to fail.
Include is ignored if the file doesn't exist, but it fails if the file does exists but contains errors.
include_once("PoliticalForum/StoredProcedure/User/number_login_attempts.php");
Above statement means the directory PoliticalForum is in the same directory of the file which has this statement.
You need to look into the directory structure you are using.
Alternatively, you could use $_SERVER['DOCUMENT_ROOT'] to locate all directories starting from root directory.
ForExample:
`$_SERVER['DOCUMENT_ROOT']`/dirLevel1 // sub directory on root
Try this
include_once "PoliticalForum/StoredProcedure/User/number_login_attempts.php";
include_once"PoliticalForum/untitled/sanitize_string.php";

php include_once not working

What would be the reasons of a non working include_once?
Here's my folder hierarchy:
/Php/Controls/GridView/GridView.php
/Php/Controls/Form/Form.php
In "Form.php":
include_once '../GridView/GridView.php';
I'm getting this error:
Warning: include_once(../GridView/GridView.php) [function.include-once]: failed to open stream: No such file or directory in ...Form.php on line 4
Warning: include_once() [function.include]: Failed opening '../GridView/GridView.php' for inclusion (include_path='.;C:\php\pear') in ...Form.php on line 4
Please tell me if you want more information.
In Form.php use
include_once dirname(__FILE__) . '/../GridView/GridView.php';
This creates an absolute path, but relative to the file, where its called from.
It can't find the file.
You should use a full path, like /Php/Controls/GridView/GridView.php instead of a relative one.
Try this:
include_once './php/Controls/GridView/GridView.php';
Hope I'm not too late on this one but I thought this might be what you're looking for:
include_once realpath(dirname(__FILE__)."/../GridView/GridView.php");
Using realpath() allows you to include files even from outside your server document root directory.

Categories