Page 1 of 1

include

Posted: Thu Feb 23, 2006 6:11 pm
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

Posted: Thu Feb 23, 2006 6:56 pm
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.

Posted: Thu Feb 23, 2006 7:32 pm
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

Posted: Thu Feb 23, 2006 8:04 pm
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';
}

Posted: Thu Feb 23, 2006 8:10 pm
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)

Posted: Thu Feb 23, 2006 8:32 pm
by feyd
code questions don't fall under the "General" topic to easy... Moved to PHP - Code.

Posted: Thu Feb 23, 2006 8:59 pm
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'

Posted: Thu Feb 23, 2006 9:07 pm
by MikeJoel
Yes I checked it and still it doesn't work.

Posted: Fri Feb 24, 2006 4:34 am
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)

Posted: Fri Feb 24, 2006 9:15 am
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

Posted: Fri Feb 24, 2006 10:00 am
by Jenk
Which is what was suggest in the first reply :?

Posted: Fri Feb 24, 2006 12:48 pm
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