include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeJoel
Forum Newbie
Posts: 8
Joined: Wed Feb 22, 2006 8:50 pm

include

Post by MikeJoel »

I have a directory structure something like


public_html/home/callMyAlbum.php

and

public_html/home/album/MyAlbum.php


in CallMyAlbum.php I have and include to album/MyAlbum.php
but MyAlbum fopens files in the same directory as Myalbum.php.

When I use the include from a different directory it crashes the MyAlbum.php because MyAlbum.php thinks it is in the same directory as the page using the include so it cant find any of the files it is trying to fopen.

Is there a way to handle this so I can include MyAlbum.php from any page in any directory? I wanted to make it usable from anywhere on my site.



Thanks
Mike
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

include '/the/full/path/to/public_html/home/album/MyAlbum.php';
Or as is often done:

Code: Select all

define('HTMLDIR', '/the/full/path/to/public_html/');
include HTMLDIR . 'home/album/MyAlbum.php';
It is also common to have a 'config.php' that contains things like the define above that all files include.
(#10850)
MikeJoel
Forum Newbie
Posts: 8
Joined: Wed Feb 22, 2006 8:50 pm

Post by MikeJoel »

I tried that but it isn't working




Here is what I have.

full path is:
home/parkswc/public_html/homestead/testarea/

in this folder I have

MyAlbum.php which opens other files in this folder (like mystuff.ini) and such.



Now I also have a path
home/parkswc/public_html/homestead/testarea/try/

In this folder I have try.php
which has an include "home/parkswc/public_html/homestead/testarea/MyAlbum.php";

So when I go to
home/parkswc/public_html/homestead/testarea/try/try.php
it doesn't work. It gets to the first fopen in the home/parkswc/public_html/homestead/testarea/MyAlbum.php and crashes because it says there is no such file.

Yet if I go to home/parkswc/public_html/homestead/testarea/MyAlbum.php is works great. This is becuase the include makes it "think" it is in home/parkswc/public_html/homestead/testarea/try/ so the paths in MyAlbum.php are incorrect.


Any ideas,
Mike
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Maybe you could do something like this...

Code: Select all

if (file_exists('include/this/file.php')) {
  include 'include/this/file.php';
} else {
  include '../include/this/file.php';
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

MikeJoel wrote: full path is:
home/parkswc/public_html/homestead/testarea/
That is your problem, that is not the full path. That path indicates that whatever directory you are currently in has a sub-directory of home, which has a sub-dir parkswc and etc..

A full path will always start with a forward slash (on *nix systems)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

code questions don't fall under the "General" topic to easy... Moved to PHP - Code.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

MikeJoel wrote: full path is:
home/parkswc/public_html/homestead/testarea/
The full path would start with a '/', which on a Unix system would probably be something like this one Linux:

/home/parkswc/public_html/homestead/testarea/

Or like this on BSD:

/usr/home/parkswc/public_html/homestead/testarea/

But you should use set the base path to your HTML directory, which is:

/home/parkswc/public_html/

And then concatenate relative paths onto that, for example:

HTMLDIR . 'homestead/testarea/mypage.php'
(#10850)
MikeJoel
Forum Newbie
Posts: 8
Joined: Wed Feb 22, 2006 8:50 pm

Post by MikeJoel »

Yes I checked it and still it doesn't work.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Use the below snippet to check if the directory exists:

Code: Select all

<?php

if (($dir = realpath('/path/to/directory'/)) !== false) {
    include ($dir . 'file.php');
} else {
    die ('Directory is invalid!');
}

?>
Adjust to match the directory and filesnames of the file you wish to include, and you can also use relative paths in realpath ('../../path/to/directory/' for example)
MikeJoel
Forum Newbie
Posts: 8
Joined: Wed Feb 22, 2006 8:50 pm

Post by MikeJoel »

This is how I got it to work.

Before the include I set a variable containing the relative path to the script. Then in the script I use that variable to insert the path before files and such.


Mike
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Which is what was suggest in the first reply :?
MikeJoel
Forum Newbie
Posts: 8
Joined: Wed Feb 22, 2006 8:50 pm

Post by MikeJoel »

Maybe... I thought it was saying to set a full server path.
I am just setting a relative path depending on the page calling it.

Like
$AlbumPath="../";
or
$AlbumPath="../../";

Or whatever.

Mike
Post Reply