Include file from diff directory?

software development

#1

Hi,

I am trying to use PHP include() to include a file that is in a parent folder of the file I am working on. I have been searching Google for 45 minutes and haven’t found anything that mentions this.

Here’s the layout of my files to sort of illustrate what I mean.

|-folder1
|–file I want to include
|–folder2
|—file I want to use the include() function to access the file in folder1

Any help would be appreciated.


#2

Try…

[color=#0000CC]include ‘…/somefile.php’;[/color]

Mark


Save [color=#CC0000]$50[/color] on DreamHost plans using [color=#CC0000]PRICESLASH[/color] promo code (Click for DreamHost promo code details)


#3

Yeah, that works. Thanks a lot.


#4

No problem, I am glad I could be of some assistance.

Mark


Save [color=#CC0000]$50[/color] on DreamHost plans using [color=#CC0000]PRICESLASH[/color] promo code (Click for DreamHost promo code details)


#5

hey… just so you know… if the file was down a couple folders, u can use…

…/…/includethis.php

each time u use …/ it drops down a level…

if you had
www.mydomain.com/includes
and the script was in
www.mydomain.com/scripts

you could do
…/includes/thisfile.php
if you were workin on a file in the scripts folder

Putting the ‘Connect’ in Connecticut… All Things Computer … Effective Office Solutions … Pioneering Computers Since the early '80s!


#6

Traditionally ‘…’ is referred to as “up” a level. But I guess it all depends on how you see the directory tree.

(I’m so old that I remember when email addresses had “!” in them rather than @ signs.)

Wholly


#7

That’s how I was taught too. The root is at the top of the tree and make your way down through the sub-directories. It pays to visualize the ‘tree’ as being upside down. :stuck_out_tongue:

Mark


Save [color=#CC0000]$50[/color] on DreamHost plans using [color=#CC0000]PRICESLASH[/color] promo code (Click for DreamHost promo code details)


#8

php can access files outside of the document root – i keep my include files one level up from the document root since that way nobody can access http://www.example.com/include.php, which really wouldn’t make any sense.


#9

I’m not that old, and still learned of a top-down tree file struct


BUGabundo :o)
http://BrinKadeiraS.BUGabundo.net
promo here
50$ discount with promo code “BUG50” on ALL plans


#10

“php can access files outside of the document root – i keep my include files one level up from the document root since that way nobody can access http://www.example.com/include.php, which really wouldn’t make any sense.”

This is exactly what I’m trying to set up right now without a lot of luck. Can you please provide an example? Or point me in the right direction?


http://www.TheHelpfulNeighbor.com


#11

If your file is in document root, (and the needed-to-be-included file is above that), just include(’…/filenametobeincluded’);

“…” refers to “Parent Folder” or, “Folder above this one”.

You can also stack …'s, like “…/…/include.php”

-vI-


#12

i have a line like this at the top of every one of my scripts – it works regardless of what directory the script is in, so i don’t have to change it if i move the script:

require_once dirname($_SERVER[‘DOCUMENT_ROOT’]) . ‘/includes/include.php’;

it starts from DOCUMENT_ROOT, which is /home/username/example.com, then dirname() takes off the /example.com, then i add on /includes/include.php which works out to /home/username/includes/include.php