1

I've the datastructure of the first image.

The code below is from Class.CIBootstrap.php

And as you can see, on the 4th line i do:

require 'Class.CIAjaxHandler.php'; 

and that works fine. Now, if it's not an ajax I want to load the proper controller...

This does NOT work. As you can see from the structure CIAjaxHandler is in the same folder as the CIBootstrap, but the CILoginController that I try to access isnt, but why cant I access it via

require '../controllers/Class.CILoginController.php' ?

structur code

7
  • What error are you getting? Commented Nov 27, 2012 at 19:55
  • Warning: require(../controllers/Class.CILoginController.php): failed to open stream: No such file or directory in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\chapAptana\classes\misc\Class.CIBootstrap.php on line 39 Commented Nov 27, 2012 at 19:56
  • Have you tried set_include_path() before the require? Commented Nov 27, 2012 at 19:57
  • Post the code will you? It should be easier than screenshooting your desktop. Commented Nov 27, 2012 at 20:00
  • You may have a permissions issue. What are the permissions on the controllers folder? Commented Nov 27, 2012 at 20:00

2 Answers 2

3

It could be a good idea define absolute path. Because:

  • Files/Folders could change location in the future
  • Prevent issues like your question (the parent file could not be the same for different call)
  • If you look the code, you know where you should find the files

So, simply include a constant file where you can generate cascade of path like so:

define('PATH_ROOT' ,__DIR__.'/'); define('PATH_CLASSES' , PATH_ROOT.'classes/'); // ... 

and in your scripts

require_once PATH_CLASSES.'controller/Class.CIAjaxHandler.php'; 

In this case you are absolute sure what you include, and WHERE is the file that you want include.

Suppose you've this structure:

/root.php

<?php echo "Hello World"; 

/index.php

 <?php include 'path1/path1.php'; 

/path1/path1.php

<?php include '../root.php'; 

In this case if you call /path1/path1.php the script works, because include root.php in parent folder and it's ok. But if you call /index.php it does not works, because the inclusion file is already relative to parent folder.

So, if in your software you plan to call different files in different locations, use constants COULD be one solution.

Another solution could be set an include path or to specify always an absolute path with __DIR__ at start of relative path.

Sign up to request clarification or add additional context in comments.

4 Comments

i never had any problem with ../ so why absolute path is good idea ? could you specify ?
@NullPointer I given 3 points... and I've not given an "absolute" rule. Sometimes it could be a good idea.
@NullPointer if you include a filel that include another file, from different other files and different locations, the ../ can give some problems
This is what I will stick to from now on.. Thanks!
2

When in doubt, use path relative to current file:

require_once dirname(__FILE__).'/../controller/Class.CILoginController.php'; 

or in php 5.3+:

require_once __DIR__.'/../controller/Class.CILoginController.php'; 

Your code works when there is a request with $_GET['page'] == 'ajax', but to what file is this request issued most probably somewhere in misc folder)? But when $_GET['page'] !== 'ajax' it may be called by other script (like index.php outside misc dir) so current working directory for php process may differ.

7 Comments

But the thing is, everything is called from index.php from the beginning, both ajax and regular
@DavidEverlöf I have the same setup - a .htaccess file routes everything through index.php, then everything goes through __autoload(). I set the set_include_path() WITHIN my __autoload() function, and it works great :)
And where is this index.php located in your tree? are you using chdir anywhere in your code?
The index.php is located in a another place, somewhere in the htdocs. I'm not using chdir, the parent of classes is "chapAptana", htdocs lies in the same folder as chapAptana, and index.php is located in htdocs/chap/index.php
@Jimbo might look intho that, never used autoload(), or Ill just stick to absolute paths
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.