10

I have 3 files: home, failed_attempt, login.

The file home and failed_attempt all refer to login file.

The annoying thing is that they throw a mistake saying that the login file doesnt exist. home will throw an exception if i do this, but failed_attempt wont.

  include_once("../StoredProcedure/connect.php");

include_once("../untitled/sanitize_string.php");

and if I do this:

   include_once("StoredProcedure/connect.php");
   include_once("untitled/sanitize_string.php");

the opposite happens, failed_attempt throws an exception , but home, wont. How do I fix this..

Do I tell the include to go up a page by putting this ../ , and therefore home.php doesnt need to go one page up therefore it throws that exception..

How can I make it so both files accept those inclueds as valid.. perhaps not relative to where they are placed.. i.e. without that ../

Directory structure:

PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files
Matrix001
  • 1,272
  • 6
  • 30
  • 51
  • 1
    Your question isn't very clear. Are the home, failed_attempt and login files in the same directory? Can you post your directory structure? How exactly do the home and failed_attempt pages "refer" to the login file? In which file does the above code appear? – daiscog Oct 20 '11 at 12:52
  • storedProcedure is my folder and it has files inside. everything as outlined.. login file has got 2 includes in it which fail to load, depending on where you call them from...homepage or login_attempt page which is inside a different directory – Matrix001 Oct 20 '11 at 13:09

5 Answers5

11

Things like realpath() and __DIR__ are your friends when it comes to creating paths in PHP.

Realpath http://php.net/manual/en/function.realpath.php

Magic constants http://php.net/manual/en/language.constants.predefined.php

Since include and require are language constructs (rather than functions) they don't need brackets. Generally speaking you'd use include to include "templates" (output files) and require to include PHP files such as classes.

So you could use something like:

$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }

(use dirname(__FILE__) instead of __DIR__ on PHP < 5)

It's worth evaluating the path before attempting to require the file as realpath() returns false if the file doesn't exist and attempting to require false; spaffs out a PHP error.

Alternatively you can just use absolute paths like:

require /home/www/mysite.com/htdocs/inc/somefile.php

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
CD001
  • 8,332
  • 3
  • 24
  • 28
  • That doesnt work too : include_once(realpath("../untitled/sanitize_string.php")); – Matrix001 Oct 20 '11 at 12:44
  • It did remove the errors. But what if realpath returns false? wouldnt it mean the file wont be included.. when does realpath returns false?? – Matrix001 Oct 20 '11 at 12:50
  • `realpath()` returns false if the path entered as the parameter doesn't exist, for example `realpath('this/file/does/not/exist');` would return false - if the file **does** exist then it'll return the real path to the given file so `realpath(__DIR__ . '/this/file/does/exist');` might return something like `/home/mysite.com/htdocs/this/file/does/exist` - e.g. the full actual path. – CD001 Oct 20 '11 at 13:11
  • I used tried and catch in __autoload function..I had no choice ..no idea of how to overcome that conflict. Why an absolute path wouldnt work...like this PoliticalForum/untitled.. why cant that path work for all files no matter what they are..I hate relative path – Matrix001 Oct 20 '11 at 13:28
  • One other way around it might be to put all your includes in a single folder and set that as the "include path" using something like `set_include_path()` (there are other ways though) ... http://php.net/manual/en/function.set-include-path.php – CD001 Oct 20 '11 at 13:37
  • hmm..sounds interesting. wont it be a bit cumbersome on the system include all those files again and again each time I refer to that file? But still it creates a problem..cause if I put files inside files , it means I have to do this ../../relativepath.php , whereas, for other files I will need to do this ../relativepath.php and for the files in the main directory or same directory as I call the include from , I need to do this relativepath.php. So that relative path is a garbage invention. is there an alternative to it? – Matrix001 Oct 20 '11 at 13:44
9

Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

1: chdir into the correct directory

<?php

// check if the 'StoredProcedure' folder exists in the current directory
// while it doesn't exist in the current directory, move current 
// directory up one level.
//
// This while loop will keep moving up the directory tree until the
// current directory contains the 'StoredProcedure' folder.
//
while (! file_exists('StoredProcedure') )
    chdir('..');

include_once "StoredProcedure/connect.php";
// ...
?>

Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

2: Use absolute paths

Now before you say this is not portable, it actually depends on how you implement it. Here's an example that works with Apache:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.php";
// ...
?>

Alternatively, again with apache, put the following in your .htaccess in the root directory:

php_value auto_prepend_file /path/to/example.php

Then in example.php:

<?php

define('MY_DOC_ROOT', '/path/to/docroot');

?>

And finally in your files:

<?php
include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
// ...
?>

3: Set PHP's include_path

See the manual entry for the include_path directive. If you don't have access to php.ini, then this can be set in .htaccess, providing you are using Apache and PHP is not installed as CGI, like so:

php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'
daiscog
  • 11,441
  • 6
  • 50
  • 62
  • I am a bit obscure about how the first method works. But I will try the second and third methods – Matrix001 Oct 20 '11 at 14:21
  • I've added comments to the first method. For more info, see [the chdir manual page](http://uk3.php.net/manual/en/function.chdir.php). – daiscog Oct 20 '11 at 14:31
6

For the beginners this will help to understand. Use simply the following based on your relative path:

//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');

OR

//include file from parent's child directory:
include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');

OR

//include file from own child directory:
include_once('StoredProcedure/connect.php');

OR

//include sibling files from same directory:
include_once('connect.php');
Reza Mamun
  • 5,991
  • 1
  • 43
  • 42
2

Do something like require(dirname(__FILE__) . '[RELATIVE PATH HERE]');

Riz
  • 9,703
  • 8
  • 38
  • 54
  • do you mean something like this: [code]include_once(dirname(__FILE__) . "../StoredProcedure/connect.php"); include_once(dirname(__FILE__) ."../untitled/sanitize_string.php"); .. it doesnt work[/code] – Matrix001 Oct 20 '11 at 12:42
  • dirname(__FILE__) will return current directory in which file having include code exists, just append relative path to this file. For ease you can print value of dirname(__FILE__) and see what 's happening. – Riz Oct 20 '11 at 12:53
  • but it doesnt work quite right include_once(dirname(__FILE__)."../StoredProcedure/User/login.php"); when I include login it makes problems ...your line of code doesnt work – Matrix001 Oct 20 '11 at 13:04
  • See comment by 'daiscog' under question and write directory structure in question. – Riz Oct 20 '11 at 13:08
  • updated. Should the _DIR_ give me the directory I am at.. so if I am at "untitled" directory in my website and home.php is in the main directory.. how does it help if I put the directory name before the relative path? – Matrix001 Oct 20 '11 at 13:27
1

The other guys have answered your question correctly, however I thought I'd contribute something else: autoloaders.

Autoloaders allow you to define a function that will automatically include certain files, when you attempt to use a class.

The documentation is here and can explain it better than I can: http://php.net/manual/en/language.oop5.autoload.php

Would really recommend using them, will save you time and is better practice.

  • I know about them..as you start the page the files are loaded..you put your includes inside them..but it doesnt solve the problem to my quesiton does it? – Matrix001 Oct 20 '11 at 12:54