require(../connect_db.php): failed to open stream: No such file or directory in C:\wamp\www\php\require.php on line 10 in wamp server. This is my first time trying to connect to mysql with php. Can anyone help me please?
You are using a relative path (../connect_db.php).
You need to take into consideration that the path is not relative to the file where the require is used, but to the file that is run.
So, if you have your main file index.php, you could have the following:
// index.php
include('config/config.php');
// config/config.php
include('config/db/connect.php'); // relative to index.php folder
include('db/connect.php') // WON'T WORK
The directory structure would be:
index.php
config/
config.php
db/
connect.php
Be sure that connect_db.php file exists in C:\wamp\www dir.
Also check folder permissions on that directory (I don't know which user uses wamp, but try first to give permissions to all users and then filter a little more).
be sure to check these following reasons:
file exists
file location is correct ( '../file') means upper location
failed to open stream: No such file or directory
Your path is incorrect.
For example, your code require('../connect_db.php'); does the following;
Go up one directory
Require connect_db.php
If we have the following tree structure, your code will work
/wamp/
www/
connect_db.php
php/
require.php
I would suggest moving connect_db.php into php/ and altering your require to become require('connect_db.php');. Or, paste your tree structure, and we can see where you're going wrong.
Related
Well, the structure of site is simple:
site.com
'config' folder
config.php
cesar.php
'login' folder
index.php
index.php
Config.php:
include_once '../config/cesar.php';
At site.com/index.php:
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
At site.com/login/index.php everything is OK.
If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?
The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
I ran into a similar problem once. You need to remember that (./config/xxx.php) and (config/xxx.php) mean the same thing but (../config/xxx.php) will go up a directory and find the config folder and xxx.php file in it.
You can also use $base_url as a prepend string to all your paths to make it clean. Where:
$base_url = 'http://' . $_SERVER['SERVER_NAME'];
Then replace your paths like
../config/xxx.php
with
$base_url.'/config/xxx.php'
I have one root folder called GASS where I put all my php files and other related folders (templates,images,js,fonts,css)inside. When i try to run my project in localhost, http://localhost/GASS/alarm_A16GSM.php everything went smoothly. I wanted to change the URL to be more specific, http://localhost/GASS/alarmsystem/16zone/A16/overview.php thus i rename the php file and put it inside folders.
GASS
alarm-system
16-zone
A16
overview
However when i try to run the new URL,the page shows error.This is the error message:
Warning: include(templates/header.php): failed to open stream: No such file or directory in .....
Code for the first URL where the page load successfully.
<div class="overview"><a href="alarm_A16GSM.php" id="overview-selected"><span>
Code for the new URL where the page shows error.
<a href="alarm-system/16-zone/A16/overview.php" id="overview-selected">
It seems like i need to configure something which i do not know what it is.
How am i going to load the page successfully using the new URL? How am i going to traverse four levels up to the root directory so that the page load successfully? Why i cannot directly call the php file using the(alarm-system/16-zone/A16/overview.php) path?
p/s: sorry for my bad English.
It looks like there is a line in your Php file, probably like
include 'templates/header.php';
Include can't find the file using that relative path, because you moved the calling file.
Probably you could change that to
include '../../../../templates/header.php';
To get back down to the GASS folder that apparently has a folder called 'templates' with a file 'header.php' that is required.
An absolute path would be good, instead but it refers to the filesystem path, not webserver path - so you'd need to know your web root folder name on the server.
Copying all the folders (templates,images,js,fonts,css) to the folder overview will solve the issue. Now there is no template file on the folder 'overview' so header.php is failed to load. Another option is create a file save all the included file path and call this file.
Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.
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);
I not sure what's going on..maybe I missed something simple.
In my connectvars.php file, I connect to the database using the variables in my config.php folder.
Here's the hierarchy:
admin(folder)
config.php
includes(folder)
connectvars.php
index.php
I want to get information from config.php to use in connectvars.php, so I use:
require_once("../admin/config.php");
But everytime I do this I get Warning: require_once(../admin/config.php) [function.require-once]: failed to open stream: No such file or directory in /home/a8879415/public_html/includes/connectvars.php on line 2
BUT when I type: require_once("admin/config.php");, it works.
I thought I had to go up a level, then go down to admin, then get config.php. So how come I just need to go into the admin folder then get config.php?
Are you running includes/connectvars.php directly or is it included by other file?
I think you are running index.php and every files are included relatively to it's path.
If you want to use relative path to other files, it can be done like that:
require_once( dirname(__FILE__) . '/../admin/config.php' );
They run in the context of the main script, so file references should originate from the main script's directory.