Closed Thread Icon

Topic awaiting preservation: include() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=13014" title="Pages that link to Topic awaiting preservation: include() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: include() <span class="small">(Page 1 of 1)</span>\

 
marf
Bipolar (III) Inmate

From: Canada
Insane since: Oct 2001

posted posted 12-10-2003 00:17

Ok on my webpage I have a bit of php which I Want to include
heres what i write

code:
<?php
include('../text/text.php');
?>



But that gives me the wrong file path. Now I think it has to do with the ../ because my server is running in linux, and the file extensions are slightly different that traditional.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-10-2003 00:28

Quick guess.

The include statement uses the local file structure not your web path.

so if your page is in /httpdocs/whatever/test.php

that will actually look for

/httpdocs/text/text.php

To find out your absolute path to the doc do this

<?php
print getcwd()
?>

Which will show you the absolute path to the file. Usually something like /var/www/yourdomain.com/httpdocs/test.php

Where the first slash is the root path of your file system so if you want to included httpdocs/text/text.php

You really need to do

include('/var/www/yourdomain.com/httpdocs/text/text.php');


I may not have explained this well but the ../ syntax is fine you just need to know what the ../ is relative to.



.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 12-10-2003).]

marf
Bipolar (III) Inmate

From: Canada
Insane since: Oct 2001

posted posted 12-10-2003 01:14

Ok i know the path to my folder is

/var/www/html/tutorials/ps/text/text.php

the file which I put the include in is

/var/www/html/tutorials/ps/tech/index.php


so within the index.php its trying to include('../text/text.php')

Why wouldn't that work? its going back one directory, in to text folder and accessing text.php right?

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 12-10-2003 03:32

Well, I don't like using the full path. If you change hosts, you might end up with a different file structure.
I like to use $_SERVER['DOCUMENT_ROOT'] to get me to the root of the site. Using that, you could use something like this:
include($_SERVER['DOCUMENT_ROOT'] . "/tutorials/ps/text/text.php");

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-10-2003 12:38

marf

That won't work because according to the paths you give ../text/ would put you in /ps/ which is one level up from /text/. The /tech/ file is parallel to the /text/ file so you can't get to it by doing ../.

-Butcher-

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-10-2003 16:18

actually, the proper way to construct a file path from the current file is using
dirname(__FILE__).
like include(dirname(__FILE__). '/../mydir/myfile.php');

- getcwd() will return the current directory, which might be something else.

Target File /var/www/html/tutorials/ps/tech/index.php

Current file /var/www/html/tutorials/ps/text/text.php
would be
include(dirname(__FILE__). '/../text/text.php');

so long,

Tyberius Prime

marf
Bipolar (III) Inmate

From: Canada
Insane since: Oct 2001

posted posted 12-10-2003 19:51

Thanks Pugzly,

The $_SERVER['DOCUMENT_ROOT'] worked.

The reason I didn't want to use a full name is because if ir changes to different servers, I don't what to have to go through every page and change the path. This way its more universal.


I'm still baffled a little as to why ../text/text.php didn't work
Start in /var/www/html/tutorials/ps/tech/index.php
1. cause ../ means go back one directory so I would then be in '/var/www/html/tutorials/ps'
2. then text/text.php would take me to '/var/www/html/tutorials/ps/text/text.php'

Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 12-16-2003 23:58
quote:
include($_SERVER['DOCUMENT_ROOT'] . "/tutorials/ps/text/text.php");



Note include() is a language construct and not a function.

code:
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include $root . '/tutorials/ps/text/text.php';
?>



And the whole point of double quotes is to allow variable interpolation.

__________________

Jon Kriek
www.phpfreaks.com

« BackwardsOnwards »

Show Forum Drop Down Menu