require_once and include problems php - php

Somehow, I cannot include the file I want, neither using require_once, nor include. The file I want to include is in the same directory as the file in which I want to include it. I tried every combination, but still does not work.
The error I get trying to use require_once
Warning: require_once(D:\PROJEKAT\wamp\www\Eif\db_connect.php): failed to open stream: No such file or directory in D:\PROJEKAT\wamp\www\Eif\create_user.php
I first tried with
require_once '/db_connect.php'
then I used
require_once '\db_connect.php' cause I realized that I have to use \ in windows
Then tried
require_once __DIR__ . '\db_connect.php'
And lastly
require_once $_SERVER['DOCUMENT_ROOT'] . '/Eif/db_connect.php';
I think that the path I'm using is good, but it keeps using it on the wrong way.
What is the problem?
Thanks

Quick answer
You should be able to do this:
require_once 'db_connect.php';
Furthermore information
require, require_once, include and include_once are all the same with one exception: require will emit a fatal error on failure where as include will emit warning.
The other thing to bare in mind these actually search through the php ini include_path directive, so when you attempt to include another file php will search through those directories in order of the first directive.
By default this directive usually looks like: .:/usr/share/php:/usr/share/pear. This could be changed by a number of functions, even the php.ini which you would want to be aware of:
set_include_path - Set the include paths directive;
get_include_path - Show the current include paths;
getcwd - Show the current working directory;
chdir - Change the current working directory;
NOTE if using set_include_path you are not adding to the list, but overriding it's value; so it would be wise to know the correct way of setting this is:
set_include_path(get_include_path() . PATH_SEPERATOR . '/path/to/new/location');

If the file you want to include are in the same directory this should work:
require_once 'db_connect.php'

There has to be some mistake in the way you are trying to reach to the path.
You can use following php functions to debug it.
<?php
print_r(get_included_files()); // Write this above the file that you are trying to include
set_include_path('path_to_your_directory');
// This will set the directory for once in<br /> your page after setting the path use require_once with the file name straight away
?>

Related

PHP Relative paths for require file

I've been going over those two topics:
include, require and relative paths
PHP - with require_once/include/require, the path is relative to what?
and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong.
Anyway this is where my problem occurred:
Root/ //this is root location for server
APP/ //this is root location for script
Root/APP/core/init.php //this is where I include classes and functions from
Root/APP/classes/some_class.php //this is where all classes are
Root/APP/functions/some_function.php //this is where all functions are
and so obviously I need to include init.php everywhere so I did in every file like this:
require_once 'core/init.php';
it was working until I have decided to create a location for admin files like this:
Root/APP/Admin/some_admin_file.php
and when I included init this way:
require_once '../core/init.php';
script failed to open functions, no such file in APP/Core/ folder
so I used DIR method presented in topic above and than even weirder thing happened, error:
no such file in APP/Core/classes/Admin/
What is that? :D I'm lost with this, could someone help a bit ;)
Include paths are relative to the current working directory, which can be inspected using getcwd(); this can be a source of many issues when your project becomes bigger.
To make include paths more stable, you should use the __DIR__ and __FILE__ magic constants; for instance, in your particular case:
require_once dirname(__DIR__) . '/core/init.php';
The dirname(__DIR__) expression is effectively the parent directory of the script that's currently being run.
Btw, __DIR__ could also be written as dirname(__FILE__).

include file within an included file

This is my directory:
global.php
includes
class_bootstrap.php
resourcemanager
index.php
To include global.php in the file index.php, I have:
require_once('../global.php');
And in global.php, i have:
require_once(./includes/class_bootstrap.php);
When run index.php, i got this message:
Warning: require_once(./includes/class_bootstrap.php): failed to open stream: No such file or directory in C:\xampp\htdocs\yurivn\global.php on line 15
Fatal error: require_once(): Failed opening required './includes/class_bootstrap.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\yurivn\global.php on line 15
I wonder if PHP search in wrong directory for the file class_bootstrap.php, it may search for "resourcemanager/includes/class_bootstrap.php" instead of "includes/class_bootstrap.php" because if I put index.php to the same directory with global.php, it works perfectly.
Is there anyway to make index.php work in resourcemanager directory without changing anything in global.php or class_bootstrap.php? I just writing some plugin, I don't want to change anything belong to the developer.
What you really want to do (to make life easier in future) is use a definitions file
As long as this is defined before your code runs then everything will be fine - easiest way is to create a definitions.php file and then include this at the top of every page you use.
define("URL", "http://yoursite.com/"); //note the trailing / to make life easier.
Then on your includes just use
require_once(URL . 'file.php');
That way on local machine transfer to new host just change the definition or URL to
define("URL", "http://siteontheinternet.com/");
and you are good to go!
it works if you write full path like
include('D:/wamp/www/ajax/a.php');
try this
require_once('../includes/class_bootstrap.php');
Easiest way would be to set ROOT_DIR in your index or config, and then use that to resolve all other include paths.
In your index.php, try use dirname(__FILE__). So it should be:
require_once dirname(__FILE__) . 'global.php';
EDIT
Since index.php is 1 down-level from global.php, so you have to add /../, so it should be:
require_once dirname(__FILE__) . '/../global.php';
You can try below:
<?php
require_once __DIR__ . '/../global.php';
And on global.php file :
require_once __DIR__ . '/includes/class_bootstrap.php';
I got the solution! Just add this before including:
chdir('./../');
and include the global.php like they were the same directory:
require_once('./global.php');
Thank you very much for trying to help me, I really appreciate it!

File linkage in PHP

I need some help in linking a file in php.
Here is what I am looking for:
I have two files process.php and index.php, both placed in different directories.
This is the full path to process.php file:
/home/happy92a/public_html/ggytg45ffs43456/wp/wp-content/themes/Funizm/loginsystem/process.php
I want to require_once index.php within the process.php file, How can I require it, here is the full path of index.php:
/home/happy92a/public_html/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php
I have already tried:
dirname(__FILE__) but it gives the path to the current file (process.php) not the (index.php) which I want to include within process.php file.
I have also tried it with $_SERVER['DOCUMENT_ROOT'] but still it does not work also I have read it is bad practice to use server variable.
i am assuming your DOCUMENT_ROOT is at /home/happy92a/public_html/ if so using that as a base to build an absolute path by doing the following.
require_once ($_SERVER['DOCUMENT_ROOT'] . "/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php");
I read that you do not wish to use DOCUMENT_ROOT. you can then establish a constant for your wordpress installation called WP_DIR
define('WP_DIR', '/home/happy92a/public_html/ggytg45ffs43456/wp/');
require_once (WP_DIR . "wp-content/plugins/plugged/index.php");
Use DOCUMENT_ROOT:
require_once($_SERVER['DOCUMENT_ROOT'] . '/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php');
try
require_once('./ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php');

Strange issue with PHP include

I am using XAMPP for PHP development, new to this, was previously familiar with WampServer. I have a require_once statement like this
require_once('config.php');
I assumed it would include the file in the current directory, but it is fetching a file from PEAR directory because the path to PEAR is also set in the include_path directive in php.ini.
However if I change the include_path to just '.' which is the current directory, it seems to work fine.
This had worked fine for me before in WampServer, no clue as to what causes this (it has always looked in the current directory before fetching form include paths). Is this a problem with PHP or something to do with XAMPP? And any solutions for this?
Well, you already found the problem: the include path directive is different.
Every include is looked up relative to the include path, the first matching file is used. If you want to explicitly use a file in a specific directory, use an absolute path:
require_once __DIR__ . '/config.php';

Using includes in php

I have a file that is called header (the header of my site).. I use that file in all my site. The problem is that it has this include in it:
include_once("../untitled/sanitize_string.php");
which means that a mistake may be thrown, depending on who calls the header file.
I have directories, subdirectories and subdirectories to the subdirectories.. is there a simple way to prevent this from happening.. Instead of taking the satize string include and placing it on every page and not in the header file
Warning: require_once(/untitled/sanitize_string.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
Fatal error: require_once() [function.require]: Failed opening required '/untitled/sanitize_string.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\PoliticalForum\StoredProcedure\User\headerSite.php on line 7
You may consider setting a global include path while using include.
For php 5.3 you can do:
include_once(__DIR__ . '/../untitled/sanitize_string.php');
where __DIR__ is the directory for the current file
For older versions you can use
include_once(dirname(__FILE__) . '/../untitled/sanitize_string.php');
where __FILE__ is the path for the current file
Lets say you have the following structure:
/app/path/public/header.php
/app/path/public/user/profile.php
/app/path/untitled/sanitize_string.php
If your header.php includes santitize_script.php with a relative path like so:
include_once("../untitled/sanitize_string.php");
the php interpreter will try to include that file RELATIVELY to the current working dir so if you will do a request like http://localhost/header.php it will try to include it from /app/path/public/../untitled/sanitize_string.php and it will work.
But if you will try to do a request like http://localhost/user/profile.php and profile.php includes header.php, than header.php will try to include the file from /app/path/public/user/../untitled/sanitize_string.php and this will not work anymore. (/app/path/public/user beeing the current working dir now)
That's why is a good practice to use absolute paths when including files. When used in header.php, the __DIR__ and __FILE__ constants will always have the same values: /app/path/public and /app/path/public/header.php respectively no matter where header.php will be used thereafter
Use absolute path...
include_once('/home/you/www/include.php');
Use absolute path as yes123 said.
include_once(dirname(__FILE__)."/../untitled/sanitize_string.php");
You're going to have to use absolute paths here, as opposed to relative. I often set up some constants to represent important directories, using the old Server vars. Like so:
define('MY_DIR',$_SERVER['DOCUMENT_ROOT'].'/path/to/yer/dir');
Then, modify your include statement:
include_once(MY_DIR.'/your_file.php');

Categories