' ../ ' not working in php - php

I have a file frontend/file.php and I want to include a file (using require function) in database/file2.php (frontend and database folders are in the same directory)
I wrote this code:
require('../database/file2.php');
But it isnt working, please help

Use this:
require __DIR__ . '/../database/file2.php';
This will ensure the file is located relative to the current path, read more about this here.
PHP Require Guide

Related

linux lamp require files

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';

PHP require_once error with relative path

I would like to test my php application in phpunit. My problem is the require_once doesn't find the file what I would like to test. I get this error:
Warning:
require_once(C:\MyProject\phpunit-tesztek\include/../include/form.php):
failed to open stream: No such file or directory in
C:\MyProject\phpunit-tesztek\include\FormTest.php on line 4
So it search the form.php file in include/../include/form.php what is wrong.
I used this code:
require_once 'PHPUnit/Autoload.php';
require_once(__DIR__.'/../include/form.php');
the Test file is in C:\MyProject\phpunit-tesztek\include\FormTest.php
and the file what I want to test is in: C:\MyProject\include\form.php
What is the problem?
The problem is the relative path. The __DIR__ uses the directory the file is in without a trailing separator. You are then changing the path with a relative path (/../) to go up one directory.
See the manual entry -> PHP Manual for __DIR__
__DIR__ will return C:\MyProject\phpunit-tesztek\include
You are looking to use the C:\MyProject\include\ path
__DIR__ . '/../../include/Form.php';
I normally use the dirname(__FILE__) myself, as it gets the current directory of the source code file (which is absolute) allowing my relative path to move from that location.
Try removing ../
require_once(DIR.'include/form.php');
Use magic constant DIR :
require_once(__DIR__ . '/../include/form.php');

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');

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');

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