PHP require failing to open stream - php

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

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.

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2
Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2
But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!
Why does this happen is this something to do with the / or the second warning line?
So I'm trying to include the settings.php file from inside the User.class.php file with the following code.
include_once("../settings/settings.php");
../ -> goes back into the root folder
settings/ -> enters settings folder
settings.php -> should locate the needed .php file
Replace
include_once("../settings/settings.php");
with
include_once(__DIR__ . "/../settings/settings.php");
__DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.
Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.
include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");
OR
define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");
Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

Relative path one level up not working in PHP

Currently I have the following structure
httpdocs
- system
- templates
Inside templates I have the following line
require_once(__DIR__.'/../system/config.php');
I get the following error
Warning: require_once(/var/www/vhosts/domain/httpdocs/templates/../system/config.php): failed to open stream: No such file or directory in /var/www/vhosts/domain/httpdocs/templates/header.php on line 8
Fatal error: require_once(): Failed opening required '/var/www/vhosts/domain/httpdocs/templates/../system/config.php' (include_path='.:') in /var/www/vhosts/domain/httpdocs/templates/header.php on line 8
As you can see in the path it just adds the two dots (../) as if it is part of the path, while I meant it to go up one level. Is there a PHP setting required to support relative paths or something?
I tried all possible variants of the path in- and excluding the DIR magic constant to no avail. And yes, config.php is present in system.
Deeply ashamed. After I moved the page to another server, I had changed the name of the system folder.

Relative path not working in PHP

I am having problems to go up and down in folders using both relative and absolute path. Everything I try giver me an error:
failed to open stream: No such file or directory in C:\wamp\www\project\Telas\Funcionario\IncluirFuncionario.php
When I try using "dirname(FILE)" it returns a warning:
Warning: include_once(C:\wamp\www\qrFoodManager\Telas\Funcionario/../../Seguranca/VerificarAutenticacaoSubTela.php): failed to open stream: No such file or directory in C:\wamp\www\qrFoodManager\Telas\Funcionario\IncluirFuncionario.php on line 2
When I try relative paths, it gives me a warning like:
Warning: include_once(../../Seguranca/VerificarAutenticacaoSubTela.php): failed to open stream: No such file or directory in C:\wamp\www\qrFoodManager\Telas\Funcionario\IncluirFuncionario.php on line 2
This is the project tree (open on eclipse Helios): http://img268.imageshack.us/img268/5593/y3l3.png
I am trying to include VerificaAutenticacaoSubTela.php in IncluirFuncionario.php; currently using Wamp Server 2.4 installed on C: .. Help please!
Use $_SERVER['DOCUMENT_ROOT']
<?php include_once($_SERVER['DOCUMENT_ROOT']."/project/Seguranca/VerificarAutenticacaoSubTela.php"); ?>
include_once(dirname(__FILE__).'/../../Seguranca/VerificarAutenticacaoSubTela.php');
Try this
<?php
include_once($_SERVER['DOCUMENT_ROOT']."/project/Telas/Seguranca/VerificarAutenticacaoSubTela.php");
?>
*Note: * Assuming the path where the files are is: /project/Telas within your wamp/www

php configuration error

My php files were working fine but today when i check them i get following errors.
Warning: require_once(Connections/deal.php) [function.require-once]: failed to open stream: No such file or directory in C:\complete project\ordering system\orderingcart.php on line 1
Fatal error: require_once() [function.require]: Failed opening required 'Connections/orders.php' (include_path='.;C:\php\pear') in C:\complete project\orderingcart.php on line 1
I don't know how to resolve these errors, where do I start?
It means, the path you gave in require_once() is invalid. You have two options,
1.Checkout whether the path Connections/deal.php is correct or not. Do you have a folder Connections in the folder where this running file is placed? Is there a deal.php inside that folder Connections?
I think your running file path is C:\complete project\ordering system\orderingcart.php. It does not have such a folder and file. So place your Connections folder in C:\complete project\ordering system\ and checkout.
2.Else, You can run the page without fatal error if you change the line to include_once(Connections/deal.php). Functions assosciated with deal.php will be inactivated.
This sounds like the file has either moved OR the file permission have been altered as to prevent the server account (usually www-data) from having access to it.
You might also have better luck by trying to access the file by suppling an absolute path, rather than a relative path, e.g:
<?php
$lines = file("C:\\Documents and Settings\\myfile.txt");
foreach($lines as $line){
echo($line);
}
?>
This file path issue give full file location like
if it is windows
require_once('C:/complete project/Connections/deal.php');
For reference
http://php.net/manual/en/function.realpath.php

Categories