-1

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.

Pepík Samků
  • 31
  • 1
  • 6
  • I'm pretty sure it's `include ("files/db.php");`. On a side note, you should **not** use short open tags ` ?>`, it will save you some pain when moving to other environments. – HamZa Nov 27 '13 at 10:51
  • possible duplicate of [How to include PHP files that require an absolute path?](http://stackoverflow.com/questions/4369/how-to-include-php-files-that-require-an-absolute-path) – HamZa Nov 27 '13 at 10:58
  • No it is different problem. read my answer conflict filename DB.php from PEAR. – Pepík Samků Nov 27 '13 at 13:08
  • The answer listed there is somewhat "universal" and way better than your methods. – HamZa Nov 27 '13 at 13:09

3 Answers3

1

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

Pepík Samků
  • 31
  • 1
  • 6
0

try this

<?php
include ("db.php");

?>

or using ./

<?php
include ("./db.php");

?>
Hans
  • 532
  • 1
  • 10
  • 26
0

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.

Florent
  • 12,220
  • 10
  • 45
  • 58
  • You might want to add an example using `__DIR__` – Darsstar Nov 27 '13 at 10:54
  • Thanks for the suggestion ;) – Florent Nov 27 '13 at 10:57
  • ok it works, but why if i only rename file db.php to db2.php now work this how can it be possible that it works with different file name. That's what I want to know. In other project i am using it and it works. – Pepík Samků Nov 27 '13 at 11:09
  • @PepíkSamků Are you sure that there is no other `db2.php` in the include path? – Florent Nov 27 '13 at 11:11
  • i make this simple example. There is only this 3 files. It is some bug or i dont know. – Pepík Samků Nov 27 '13 at 11:16
  • @PepíkSamků it's just impossible. A typical [user error](http://en.wikipedia.org/wiki/User_error). Make sure to show hidden files. It's just tiresome to help troubleshoot such trivial things. – HamZa Nov 27 '13 at 11:18
  • http://us3.php.net/get_included_files can be used to see which db2.php was included. Like @HamZa said, this should be a user error. – Darsstar Nov 27 '13 at 11:35
  • OMG I know.. PEAR DB.php something like shadowing variables so there shadowing included files thanks for that get_included_files(); – Pepík Samků Nov 27 '13 at 11:52