2

There's a site I'm developing, and I don't really understand the problem that is occurring...

There's a link inside a table in the Home page. When I click on it, It is supposed to provide some GET parameters to the hyperlinked page. The receiving page processes it, updates the database and redirects to the Home Page.

I've included some necessary php files as "require_once()" in the Home page. But I can't do it on the processing page. It gives some warnings. I don't really understand why and I don't know the solution to this problem. Please help!

Code in the Home page:

<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header_main.php"); ?>

<?php
 echo "<td><a href='includes/process.php?id=".$arr['id']."'>Process</a></td>";
?>

<?php include("includes/body_footer_main.php"); ?>
<?php require_once("includes/db_connection_close.php"); ?>

Code in the Processing Page:

<?php require_once("includes/db_connection_open.php"); ?>
<?php require_once("includes/functions.php"); ?>

//Processing codes

<?php require_once("includes/db_connection_close.php"); ?>

The warnings I'm getting are:

Warning: require_once(includes/db_connection_open.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\MySite\includes\process.php on line 1

Fatal error: require_once() [function.require]: Failed opening required 'includes/db_connection_open.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\MySite\includes\process.php on line 1

maxxon15
  • 1,559
  • 4
  • 22
  • 35
  • I have read before, *require*, most specifically **require_once** has some known issues under Windows. Try using full path to the file. – Rolice Oct 28 '11 at 10:19
  • @Rolice : I don't think so, it works, but I think the problem is something else... Watch my comment in the answers below Seriously, Its getting a bit confusing! :-( – maxxon15 Oct 28 '11 at 13:07
  • Yes, there are workarounds. :) The code below is generating full path to include. ;) On *UXes, your code should work as you have pasted it above. This is just a hint. – Rolice Oct 29 '11 at 08:24

2 Answers2

2

It seems like you are currently in the includes directory because your error says 'in C:\xampp\htdocs\MySite\includes\process.php on line 1'.

However you are still trying to require within includes/ so you end up in includes/includes/ where your files aren't at.

If you are having troubles finding the correct path because you have a file that is loaded through inclusion as well as AJAX for example you can use __DIR__ (or dirname(__FILE__) in older PHP installations) to make sure you have the correct path.

So in process.php that would be for example:

require_once __DIR__.'/db_connection_open.php';
Kokos
  • 9,051
  • 5
  • 27
  • 44
  • 1
    Dutchman? The word "map" is probably supposed to be "directory" right? :) – toon81 Oct 28 '11 at 09:52
  • @Kokos : It does work in the way you've mentioned. But the **db_connection_open.php** file also has `require("includes/connection_constants.php);` in it. The **db_connection_open.php** file is also _required once_ in the Home page. So why, while loading the Home page; no error is showing? – maxxon15 Oct 28 '11 at 13:04
  • **db_connection_open.php** is required from within your index file which is in the root (or at least, one level up from **includes/**). So the include_path for **db_connection_open.php** is at that moment the root, that's why it will work. However as soon as **db_connection_open.php** gets loaded separately its include path will be **includes/**. By requiring starting from `__DIR__` you avoid any confusion as to what your current include path is. – Kokos Oct 28 '11 at 13:25
  • I used to have so much problems with this, always doing checks for file_exists and stuff to get the correct path. `__DIR__` is a lifesaver ^^ – Kokos Oct 28 '11 at 13:57
0

Well, the errors seem to indicate that the files don't exist at the location you're trying. By the look of things, the process.php file already resides in the includes directory, so on your require_once() statements on the page, you need to update the links as follows:

<?php require_once("db_connection_open.php"); ?>
<?php require_once("functions.php"); ?>
//Processing codes
<?php require_once("db_connection_close.php"); ?>

Hope that helps!

BenM
  • 52,573
  • 26
  • 113
  • 168
  • Hmm... Seems to work. Now I have some debugging to do. But I don't understand one thing. Why do we need to remove the "**includes/**" part? When I used same code - `require("includes/connection_constants.php");` in **db_connection_open.php** file, it worked fine, the Home page functioned properly. – maxxon15 Oct 28 '11 at 10:00
  • You need to remove the `includes/` directory because your file already resides in that path. PHP file includes are relative of the current path. It would function properly from the homepage, because it is in the parent directory (so `includes/` resolves correctly). – BenM Oct 28 '11 at 10:03
  • Some of the files I have in the includes folder : **db_connection_open.php** **functions.php** **process.php** **connection_constants.php** **db_connection_close** Of these, **db_connection_open.php** has `require("includes/connection_constants.php);` The **db_connection_open.php** page is also _required once_ in the Home page. Then why while loading the Home page, No error is shown...?? – maxxon15 Oct 28 '11 at 12:58