PHP include not loading file - php

Im runninng lamp stack locally on my ubuntu 16.04 OS and Im getting problems when including files. My code in index.php:
<?php include('steamauth/steamauth.php'); ?>
<?php include('steamauth/userinfo.php');?>
my structure is:
index.php
steamauth
steamauth.php
userinfo.php
Im getting the following error:
include(steamauth/userinfo.php): failed to open stream: No such file or directory in /var/www/html/index.php
include(): Failed opening 'steamauth/userinfo.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/index.php
I tried using the full path , adding webroot before the path , changing include_path in php.ini , but nothing seemed to work . what do i do ?

Check folder pemission
Check that the Apache user has access to the include folder.
Check file permissions
Check that the permissions on the files that you are including can be read and executed by the Apache user.
Check PHP settings
It could be caused by one of the following PHP settings:
open_basedir:
If this is set PHP won't be able to access any file outside of the specified directory (not even through a symbolic link).
safe mode:
If this is turned on restrictions might apply.
Make path absolute
Check that $_SERVER["DOCUMENT_ROOT"] is correctly set for your domain by using:
print $_SERVER["DOCUMENT_ROOT"]; // Add to top of you index.php file.
Then change your includes to:
$_SERVER["DOCUMENT_ROOT"] . '/sub_folder/file_to_include';
Check for typos and spelling mistakes
Carefully check that what you think you have called the folder and what you are using to include it are spelt the same.
BTW: You can just use the following in your index.php file:
<?php
include('steamauth/steamauth.php');
include('steamauth/userinfo.php');
You also don't need to add the closing ?> at the end of the file - it's actually good practice not to.

Related

Warning: require_once(/HTML/Template/ITX.php): failed to open stream: No such file or directory in C:\wamp64\www\mysite\main\login.php on line 13 [duplicate]

I was writing an web app in PHP, when I encountered a strange situation. To illustrate my problem, consider a web app of this structure:
/
index.php
f1/
f1.php
f2/
f2.php
Contents of these files:
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("../f2/f2.php"); ?>
f2.php: blank
now when I try to open index.php in my browser I get this error:
Warning: require_once(../f2/f2.php) [function.require-once]:
failed to open stream: No such file or directory in /var/www/reqtest/f1/f1.php on line 2
Fatal error: require_once() [function.require]:
Failed opening required '../f2/f2.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/reqtest/f1/f1.php on line 2
Is there something obvious I'm missing? how do include paths work in PHP?
Before I asked this question, I attempted to experiment and find out. I set up another test, like so:
/
index.php
f1/
f1.php
f2.php
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("f2.php"); ?>
f2.php: blank
To my surprise (and utter confusion), this worked out fine!
So, what is the secret behind the path resolution?
PS I saw this question, but it still does not answer the second case that I've stated here.
If you include another file, the working directory remains where the including file is.
Your examples are working as intended.
Edit: The second example works because . (actual directory) is in your include path (see your error message).
Edit2:
In your second example, the key point of your interest is this line:
<?php require_once("f2.php"); ?>
At first it will look in the current working dir (/var/www/req_path_test), but does not find f2.php.
As fallback, it will try to find f2.php in your include_path ('.:/usr/share/php:/usr/share/pear'), starting with '.' (which is relative to the actual file, not the including one).
So './f2.php' works and the require does not fail.
When you open index.php, working dir is set to the folder this file resides in. And inside insluded f1.php this working dir does not change.
You can include files by using their absolute paths, relative to the current included file like this:
require_once(dirname(__FILE__).'/../../test/file.php')
But better consider using an autoloader if these files contain classes.
Normaly in you old structure
<?php require_once("f2/f2.php"); ?>
instead of
<?php require_once("../f2/f2.php"); ?>
should work. As far as i know php takes the paths from the initial script
It sounds like your server has the open_basedir setting enabled in the PHP configuration. This makes it impossible to include (and open) files in folders above your in the directory structur (i.e., you can't use ../ to go up in the folder structure).
From the PHP Docs PHP include
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
If the file path is not given then i.e require_once("f2.php");
1st. The include_path is checked
2nd. The calling scripts own directory is checked
3rd. Finally the current working directory is checked
If file not found then PHP throws warning on file include & fatal error on require
If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.
If you include/require your file beginning with . or .. or ./ then PHP's parser will look in the parent directory which is the current working directory i.e require_once("../f2/f2.php"), php will check at the root directory as the calling script index.php is in that directory.
Now You have not defined any include path in your PHP script thus it always falls back to the calling script and then into the current working directory.
// Check your default include path, most likely to be C:\xampp\php\PEAR
echo get_include_path();
// To set include path
set_include_path ( string $new_include_path ) : string
The Current Working Directory is derived from your main calling script index.php.
// The Current Working Directory can be checked
echo getcwd();
In the first Example where the required file "../f2/f2.php" is from f1.php
You code does not work because -
The specified path is ignored by PHP as your filename begins with ../
f1/ the calling script's own directory is ignored as well.
The parser directory looks into the parent directory to find the requested file. The current working directory is root directory, this is from where you have initiated the working script index.php. The file is not located at this directory, wrong path given.
Thus you get the Fatal Error
In the Second example you have changed the directory & from f1.php you require_once("f2.php").
Your code works because -
This time you require("f2.php") no leading ../ or ./ This time PHP checks the include_path but does find it there, as you haven't defined it and the file does not reside in the default preset include_path.
This time the calling script f1.php's directory is f1/. and you require file ("f2.php") is located at this directory. PHP This time checks the file in this directory and finds it.
PHP does not have to check the working directory as the file was found.
Thus Your Code Works Fine!

Cannot set PHP include_path

I have uploaded all my files in var/www/html and in one of my php files I have this line :
require_once('libraries/stripe/init.php');
the structure of my folders are list this:
www
-html/
-libraries
-> Stripe -> init.php
-register.php
I keep getting this error message:
Warning: require_once(libraries/Stripe/init.php): failed to open stream: No such file or directory in /var/www/html/register.php on line 116
Fatal error: require_once(): Failed opening required 'libraries/Stripe/init.php' (include_path='.:/usr/share/php:/var/www/html/') in /var/www/html/register.php on line 116
my php.ini file used to be like this
include_path= ".:/usr/local/php/pear/"
but based on some answers here i changed it to
include_path='.:/usr/share/php:/var/www/html/'
but it's not working!
Edit: in my libraries folder I have file called index.php the content is:
<?php
header("Location: ../"); die();
I wouldn't leave library paths to chance like that:
require_once(__DIR__ . '/libraries/Stripe/init.php');
This would make sure you include the script using the absolute directory path of the currently running script.
Update
Failed opening required '/var/www/html/libraries/Stripe/init.php'
Well, then the file is simply not there; if this file was generated by some other tool, e.g. composer, it would need to be done again on a new server (or ideally at every deployment).
i think the problem is my Stripe folder is in "s" but I'm using capital "S". is there anyway to make not case sensitive?
File systems under Linux are case sensitive by default (not even sure whether it can be changed) as opposed to Windows. Make sure you use capitalisation consistently.
That not what you are looking for. it says that php.ini file can't find the path of the file that you specified. that's why you should use absolute path to the file
require_once "path/to/file";

require_once and include are having php.ini issues

Hi I am getting this error, when I run the index.php file which includes
require_once("system/config.php");
I am wondering what does this message mean, and how do I go about fixing such an issue. As this site worked fine when I ran it directly on my Mac.
require_once(): Failed opening required 'system/core.php' (include_path='.:/usr/share/php')
ALL THE BELOW ANSWERS ARE CORRECT!
However my issue was a CHMOD issue I had 644 instead of 744
It usually means you're trying to include the file with an incorrect path. What happens when you try the full path?
require_once(getcwd() . 'system/config.php');
The way your script is now, it's looking for the file with a directory structure like this:
file.php <- is calling the require_once()
- system <- the system directory
- config.php <- the config file
But this isn't the path where it's located.
The file system/core.php needs to be relatively to the file which is declaring the line require_once("system/core.php"); (that's system/config.php) or the direcory that contains system must be in your include_path. Search for core.php on your Mac. Check the configuration settings (include_path) on your Mac.

Error when using php include function

I'm working on a php web application under windows. I have many script files placed in their respective folders and all the files needs a file named 'connexion.php' that is situated in the root project.
The structure of the project is as follow:
Projet_beta :
-client (folder):
*addClient.php
*modifyClient.php
*deleteClient.php
- connexion.php
So, when i use include ('../connexion.php') inside the 'addClient.php' it displays this error message :
Warning: include(connexion.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\project_beta\client\addClient.php on line 3.
How can i fix this problem ?
Thank's every one.
Try this include ('/../connexion.php')
The extra slash should solve your issue.
Check in your httpd.conf file :
left click on wamp
php menu
open the httpd.conf file
Find the line starting with DocumentRoot
Change the line to DocumentRoot "c:/wamp/www/" and save the file
Restart wamp services
Then use :
include($_SERVER['DOCUMENT_ROOT'] . '/project_beta/connexion.php');
Should use
dirname(__FILE__)."../connexion.php"
instead.
In this case, "../" search files from the path you executing the file, not the real path of the file.
You can use dirname func :
include(dirname(__FILE__)."/connexion.php"));
Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled.

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