Include cant find directory PHP - 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.

Related

PHP require failing to open stream

I am struggling trying to figure out why my require path link is not working.
My folder setup is the following:
Main domain on server test.example.com.
public_html -> test.example.com -> PHPMailer -> PHPMailerAutoload.php
Since the main domain is test.example.com, I am putting test.example.com in the path url. I have to do this for any link on the site (it was a setup mistake, but I haven't changed it yet).
The way I am trying to access this folder is below:
require 'test.example.com/test.example.com/PHPMailer-master/PHPMailerAutoload.php';
I get the following errors:
Warning: require(test.example.com/PHPMailer-master/PHPMailerAutoload.php): failed to open stream: No such file or directory in /home/example/public_html/test.example.com/php/newsletterSend.php on line 4
Warning: require(test.example.com/test.example.com/PHPMailer-master/PHPMailerAutoload.php): failed to open stream: No such file or directory in /home/example/public_html/test.example.com/php/newsletterSend.php on line 4
Fatal error: require(): Failed opening required 'test.example.com/test.example.com/PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/opt/cpanel/ea-php71/root/usr/share/pear') in /home/example/public_html/test.example.com/php/newsletterSend.php on line 4
I need the link to be an absolute path because the file will be used in different folders.
Does anyone see what I am doing wrong?
it looks like your php is in a directory above the phpMailer folder.. so i think the path would be "../PHPMailer/PHPMailerAutoload.php"
you can use absolute paths if its simpler:
/home/foo/public_html/test.example.com/PHPMailer/PHPMailerAutoload.php
or relative paths if you know where you're starting!
../PHPMailer/PHPMailerAutoload.php

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.

PHP include - failed to open stream: No such file

I wrote a website in HTML but then I wanted the header and navbar portion in one file so I just had to change it once. I changed all the pages to .php and added the php includes. However now I get an error on pages that I've put in folders so I could create cruftless links.
Header file: /test/assets/header.php
Works: /test/index.php contains <?php include 'assets/header.php'; ?>
Throws Error: /test/company/index.php contains <?php include '/test/assets/header.php'; ?>
Error:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I am having an issue linking to the header file that is in a folder in the root folder. I believe it is a simple issue of just not knowing how to input the URL. If I attempt to include the full path to the header.php I get a URL file-access is disabled error
If you start a path with a / then it's an absolute path. Absolute mains to the actual filesystem root directory, not the virtual document root. This is why it fails.
Commonly this is what was meant:
include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
// Note: array key quotes only absent in double quotes context
I was also facing the same warnings:
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
I solved them with the following solution:
You have to define your path then use include function as below:
define('DOC_ROOT_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
require DOC_ROOT_PATH . "../includes/pagedresults.php";
After that, you can use any directory structure.
It's not a URL; it's a filepath (although you can use full URLs when the server configuration allows it).

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";

Categories