-1

I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.

Here's the file structure:

  • appCode
    • db.php (File I'm trying to include)
  • studentManagement
    • index.php
    • dep
      • getData.php (File I'm trying to include db.php into)

I want to include appCode/db.php in studentManagement/dep/getData.php.

getdata.php is executed with ajax from index.php

When I use:

require_once("../../appCode/db.php");

It doesn't work.

The only way it works is it I change directory first:

chdir("../");
require_once("../appCode/db.php");

Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.

Any help appreciated!!

mickzer
  • 5,958
  • 5
  • 34
  • 57

2 Answers2

0

that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)

in fact chdir has nothing to do with it, and this:

require_once("../appCode/db.php");

is the right way to call it.

always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php

  • Sorry I should have mentioned that getdata.php is executed with ajax from index.php. So the above doesn't explain it. If I remove chdir("../"); , the script fails, the file isn't included. – mickzer Jul 12 '14 at 21:44
  • @mickzer can you update your question with this very relevant information and most importantly the exact path that is called with ajax? – Félix Adriyel Gagnon-Grenier Jul 12 '14 at 21:47
  • I have. Ajax is used in index.php to execute dep/getData.php – mickzer Jul 12 '14 at 21:47
  • ajax is a concept used from a local brower. php file do not execute ajax. can you post the actual ajax call (from javascript of jquery) with his parameters. does it point directly to index.php? – Félix Adriyel Gagnon-Grenier Jul 12 '14 at 21:51
  • Ajax allows for a request to be made to the server for a file without the current page reloading. In this case it's getData.php, which in order to be returned to the browser has to be executed by the server. The url parameter of my ajax request is url: "dep/getData.php". This ajax request takes place on index.php – mickzer Jul 12 '14 at 21:55
  • sorry, good may be a bit harsh. I mean that in you file structure AppCode has a capitalized `A` but not in the `require`. And btw, thanks for that ajax crash course, but I myself is proficient with it, while you seem to have some confusion. AJAX is not even code... it doesn't `take place` on a php file... :) – Félix Adriyel Gagnon-Grenier Jul 12 '14 at 21:58
  • 1
    Thanks for pointing out the capitalized A. I too am pretty familiar with Ajax, And I also never once mentioned that it takes place on a php file :) What I meant was that the javascript to perform the ajax request is located on index.php inside a – mickzer Jul 12 '14 at 22:06
  • Great. Now if while you are calling directly index.php and the `require_once("../appCode/db.php");` does not work, there is something you are not telling us, which is why I would like to actually see the content of your script with **the ajax request!!!** and maybe even the code which takes care of the ajax *call*, with the lines surrounding the failling require :) – Félix Adriyel Gagnon-Grenier Jul 12 '14 at 22:12
  • I don't you've read my question clearly enough. The ajax request, and the script itself both work fine when I use chdir("../");. The question I'm asking is why does chdir("../"); work and not require_once("../../appCode/db.php");. The code performing the ajax request is irrelevant. It gets the correct output. The correct output is also echoed out when I pass a query string through the url for getData.php. – mickzer Jul 12 '14 at 22:22
0

If you know your entire directory all the way from root you can use:

http://php.net/manual/en/function.set-include-path.php

Now instead of using relative paths you can always include or require files starting at your include path.

You could put this in getData.php:

set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';

You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.

Damian C
  • 2,111
  • 2
  • 15
  • 17
  • This doesn't answer the question. I'm trying to get an explanation for the behaviour illustrated above. It works for me with a work around O just want to know why. – mickzer Jul 12 '14 at 22:43