include not work (PHP) - php

i have strange problem with include commnad
my files structure
/index.php
/files/init.php
/files/db.php
and sources of this files
index.php
<?
include ('files/init.php');
?>
init.php
<?
include ("db.php");
?>
Source from db.php is not executing.. NOW if i rename file db.php to db2.php and rename it in init.php NOW IT WORKS
there is some php cache or what ? i dont understand it.

conflict filename DB.php from PEAR.
DB.php from PEAR is included instead of your "db.php"..
So use filename other than "db.php".
EDIT or use include with DIR macro

try this
<?php
include ("db.php");
?>
or using ./
<?php
include ("./db.php");
?>

The included files are resolved according to the entry point (here index.php).
Moreover, require is better is the file MUST exist.
index.php
<?php require 'files/init.php'; ?>
files/init.php
<?php require 'files/db.php'; ?>
Update 1: As suggested by #Darsstar, here is an example using __DIR__:
files/init.php
<?php require __DIR__.'/db.php'; ?>
Update 2: Maybe you should consider PSR-0 or PSR-4 to deal with file inclusion.

Related

How to include a file using a relative path?

I would like to include a file located in front/header/header.php from a front/user/login.php file.
How i can include header.php in login.php ? since it needs to step back from user folder and then enter to header folder.
i tried :
<?php include('/header/header.php'); ?>
<?php include('../header/header.php');?>
<?php include('../../header/header.php');?>
<?php include('header/./header.php');?>
<?php include('header/../header.php');?>
<?php include('front/header/./header.php');?>
<?php include('front/header/../header.php');?>
I think, you missed context of including path. Try to use:
include dirname(__FILE__) . '/../header/header.php';
It should helps.

How to include php db config file form sub directory

I want to use this database config file which located in-> "/main/config.php" directory. Now please tell me how can i include that config.php from-> "/main/admin/login.php" file.
i have simply tried following but not worked-
<?php
include "/main/config.php";
?>
thanks in advance
If you are trying to go back to the file directory then this will include the file. However you have missed the brackets.
<?php include("../main/config.php"); ?>

PHP Include Issue - Two includes don't work?

I've been having issues with my PHP include function. So can anyone explain to me why this would work...
//testfile1.php
<?php
include 'file1.php';
?>
//testfile2.php
<?php
include 'file2.php';
?>
but this will NOT work..
//testfile.php
<?php
include 'file1.php';
include 'file2.php';
?>
Is there anyway to include the second file within the main file? I'm suppose to streamline two php files into one main file and run that. (All files are on the same directory and can 'see' each other no problem.)
Try changing your include statements to include_once. Look for includes within your included files and change them also.
If file1.php also includes file2.php itself, and file 2 had a function declaration, you could be re-declaring a function with the same name.
Looks like you're overlapping include with files which are probably being included twice or not at all. In this case you need to use either
include_once() instead of include()
You can also use
require_once() instead of include() //use this option if the include is required

include files from subdirectory which calls from another directory

I have a file here: public_html/wiki/index.php
Im calling these files:
<?php
include "../auth.php";
include "../header.php";
?>
However, my page looks like this:
If I paste the same file in public_html/index.php then it will look like this:
I think I've narrowed it down to my header.php file which is being called from /wiki/ but header calls files from ../css.. and ../js and so on.
How can this be adressed? I've looked at many posts already which tells me to define a global variable but it doesn't help me. Like so:
<?php //config file in root folder
define("ROOT", __DIR__ ."/");
?>
and then calling them with this:
<?php
include_once("../config.php");
include (ROOT ."auth.php");
include (ROOT ."header.php");
?>
How should I do this so I can get all my javascripts and the actual site with me in another folder?
NOTE: I WANTED THIS AS A COMMENT BUT DO NOT HAVE THE REP
We have a different folder structure, but it is good practice to put your includes in one folder and from there link all the JS files (best practice is, place them in a pre-footer for speed) and then do
<?php include $_SERVER['DOCUMENT_ROOT'].'/includes/page-footer.php'; ?>
Just change the file name to suit you :) hope this helps
the problem was my include file didnt specify links as /js/file.js
i had them as js/file.js.
thanks #cd001

Manage multiple include files in PHP

I have a main.php file which has the following three included php files:
<!--TAB 1--->
<?php include "tab1.php"; ?>
<!--TAB 2--->
<?php include "tab2.php"; ?>
<!--TAB 3--->
<?php include "tab3.php"; ?>
When I open main.php in Dreamweaver, I have access to these 3 included files. My question is: What about files that are included inside one of these include files?
For example, the tab2.php file reference these php files:
<?php include "a.php"; ?>
<?php include "b.php"; ?>
<?php include "c.php"; ?>
In order to be able to edit these included files which are inside another include file, what needs to be done? Will I need to open those directly?
What I would like to be able to do is open main.php and then be able to add all 6 included files:
tab1.php
tab2.php
tab3.php
a.php
b.php
c.php
It seems as though I only have direct access to the first-level include files. Any suggestions? Thanks.
If you're using Dreamweaver CS5, it has an (default enabled) option to show includes in a line below the file tabs. If you have an include that has other includes, the only way of editting this includes directly from this file is to press CTRL+D in the include, then Dreamweaver will show a popup for you to select the includes to edit and it will open the selected one in a tab.

Categories