linux lamp require files - php

Not long ago I started using linux as OS principal. But I have a problem to include files.
When I try include a file which in turn includes a third file, I end up getting a error: "failed to open stream: No such file or directory in /var/www/html/..."
For example, my files are distributed in this way:
and code of the 3 files is:
primero.php
require_once '../B/c/segundo.php';
segundo.php
require_once '../d/tercero.php';
tercero.php
echo 'success';
Error:
can someone explain to me what happens? this in windows works. and the truth is I would avoid using "dirname(FILE)"
PD: sorry, I can't post images

Using relative path, sometime is painful. Therefore what I start doing is creating a settings.inc.php file which I keep my important paths. For instance:
try this:
settings.inc.php
<?php
define("ROOT_PATH" , dirname(__FILE__) . "/");
define("CLASS_PATH", ROOT_PATH . "class/");
?>
Then, I require the settings file in the header.php (so I can have it on every page)
require_once("settings.inc.php");
And I use it like
require_once(ROOT_PATH . 'B/c/segundo.php');
And
require_once(ROOT_PATH . 'd/tercero.php');

Using __DIR__ in front of require makes the required path relative to the file in which it is defined. For example:
require_once __DIR__.'/../d/tercero.php';

Related

require_once and include problems 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
?>

include files from anywhere on the server

I have website running perfectly on production server. I have moved it to another web server. (VPS).
Let me explain you with example:
The directory structure:
includes/
header.php
business/
index.php
some other files...
index2.php
In my previous version, I used
include_once(includes/header.php)
in index.php and index2.php. It runs fine. but in my new server setup it's giving me error regarding path (obvious).
ERROR:
include_once(includes/header.php): failed to open stream: No such file or directory
And because of that:
Fatal error: Class 'EncryptionClass' not found
I think there are some server configurations which I need to do. But, I don't know how?
Please guide me. Let me know if you want more information.
if using PHP 5.3+ Try using:
include_once(__DIR__.'/includes/header.php');
DIR is a magic constant that will return the full directory that the file is in.
You could supply an absolute file system path to the include:
include_once($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");
I would simply add your includes directory to the include_path. For example, in index2.php
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/includes', // relative to this file, index2.php
get_include_path()
]));
include_once 'header.php';
and similarly in business/index.php...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/../includes', // relative to this file, business/index.php
get_include_path()
]));
include_once 'header.php';
Personally, I would use PSR-0 file-to-class name mappings and configure an autoloader, eg
includes/EncryptionClass.php
class EncryptionClass { ... }
index2.php
spl_autoload_register(function($class) {
require_once __DIR__ . '/includes/' . $class . '.php';
});
$encryptionClass = new EncryptionClass();

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!

PHP require path - same directory

I have index.html in the root and all supporting files in /HTML. I have Google analytics code in a file in the HTML directory. It works from my index.html with this code in between the head tags...
<?php
require('HTML/GoogleAnalytics.html');
?>
but not in any of the supporting files in the HTML directory, same directory as the file i'm trying to require/include with this code...
<?php
require('GoogleAnalytics.html');
?>
from PHP.net "...include will finally check in the calling script's own directory and the current working directory before failing"
What am I doing wrong?
From PHP 5.3 (which is at this time after end of life cycle) and later you can use also __DIR__ constant , http://php.net/manual/en/language.constants.predefined.php
require(__DIR__ . '/GoogleAnalytics.html');
To make it relative to the current file, you can prepend dirname(__FILE__) like so:
require(dirname(__FILE__) . '/GoogleAnalytics.html');
By default, paths are relative to the file that the request originated from.
Try using this:
require('/HTML/GoogleAnalytics.html');

How do I include a PHP script from a base path other than the current script?

I have a PHP application I'm trying to include, but it's causing problems because I'm trying to include its files from a subdirectory. For instance, here's the directory structure (more or less) I'm trying to work with:
/site
/scripts
/local
some_script.php
lib_file_a.php
lib_file_b.php
lib_load.php
lib_load.php has an include command for files lib_file_a.php and lib_file_b.php (relative path).
What I want to accomplish is to include lib_load.php from some_script.php, but there's a problem with the way I'm including lib_load.php. At first I tried require('../../../lib_load.php');.
As you would expect I saw the error, "Failed opening required 'C:\xampp\htdocs\site\scripts\local\lib_file_a.php'". As you can see, PHP is trying to include a file that does not exist. I found a Stack Overflow post that told me about using an absolute path (link), but this did not help. I next tried this code:
require(realpath(dirname(__FILE__) . '../../../' . 'lib_load.php'));
However, this time, the same error comes back. What am I doing wrong?
EDIT: I realized that the issue was actually in the library loader, which turned out to be including files based on an absolute path, not a relative path. (I should have read more carefully.) I resolved the issue by changing the path used by the library. This question should be closed if a mod sees this request.
To include lib load
require $_SERVER['DOCUMENT_ROOT'] . '/scripts/lib_load.php';
inside of lib load
require dirname(__FILE__) . '/lib_file_a.php';
if I got your problem right
Try this:
require dirname(__FILE__) . '/../../../lib_load.php');
Or if you're using PHP 5.3 or higher use this instead (it looks nicer):
require __DIR__ . '/../../../lib_load.php');

Categories