Jump to content

Whats the difference?


Bman900

Recommended Posts

include('./file.php');

or

include('../file.php');

 

I have to scripts that both use the include function. 1. root/pandora/include/contants.php which includes

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

 

Than I have root/demo/admin/admin.php which includes 

<?php include("../include/session.php");
include("../include.php");
include("../config.php"); ?>

 

In both of those scripts I need to go back one folder. In the first one it only work with one . while in the other one it only work with two .    Here are the errors I get:

 

First script if I add the two dots:

 

Warning: include(../include.php) [function.include]: failed to open stream: No such file or directory in /home/balint/public_html/pandora/include/constants.php on line 2

 

Warning: include() [function.include]: Failed opening '../include.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/balint/public_html/pandora/include/constants.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/balint/public_html/pandora/include/constants.php:2) in /home/balint/public_html/pandora/include/session.php on line 50

 

Code at line 2:

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

 

Second script if I have only one dot errors:

 

Warning: include(./include/session.php) [function.include]: failed to open stream: No such file or directory in /home/balint/public_html/demo/admin/admin.php on line 3

 

Warning: include() [function.include]: Failed opening './include/session.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/balint/public_html/demo/admin/admin.php on line 3

 

Warning: include(./include.php) [function.include]: failed to open stream: No such file or directory in /home/balint/public_html/demo/admin/admin.php on line 4

 

Warning: include() [function.include]: Failed opening './include.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/balint/public_html/demo/admin/admin.php on line 4

 

Warning: include(./config.php) [function.include]: failed to open stream: No such file or directory in /home/balint/public_html/demo/admin/admin.php on line 5

 

Warning: include() [function.include]: Failed opening './config.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/balint/public_html/demo/admin/admin.php on line 5

 

And to clarify here is the code on those line:

 

<?php 

  session_start();
  	include("./include/session.php");
include("./include.php");
include("./config.php");

?>

 

Any ideas why this behavior is happening?

Link to comment
Share on other sites

From the php.net documentation for the include() function -

 

If filename begins with ./ or ../, it is looked for only in the current working directory or parent of the current working directory, respectively.

 

One dot refers to the current working directory (which is not necessarily the current directory if you have more than one level of include statements.) Two dots refer the parent of the current working directory.

 

Only none, one, or two dots have any meaning in file system paths.

 

The default current working directory is that of the main script that was requested. If the main script includes a file that is in some other folder than the main script and that file then includes another file, the last include is relative to the folder that the main file is in.

 

You may in fact find that using absolute file system paths, formed using -

 

$_SERVER['DOCUMENT_ROOT'] . '/actual_path/your_file.php'

 

eliminates all ambiguity about where a file is being included from.

Link to comment
Share on other sites

Try the entire path.  Then it will work in all cases and all directories..

 

<?php

include("/root/pandora/include/session.php");

?>

 

You may also want to look into "require once" as in some cases you need to make sure the file is there.  If not then don't even bother with the rest of the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.