include
Moderator: General Moderators
include
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Code: Select all
include '/the/full/path/to/public_html/home/album/MyAlbum.php';Code: Select all
define('HTMLDIR', '/the/full/path/to/public_html/');
include HTMLDIR . 'home/album/MyAlbum.php';(#10850)
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
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
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';
}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..MikeJoel wrote: full path is:
home/parkswc/public_html/homestead/testarea/
A full path will always start with a forward slash (on *nix systems)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
The full path would start with a '/', which on a Unix system would probably be something like this one Linux:MikeJoel wrote: full path is:
home/parkswc/public_html/homestead/testarea/
/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)
Use the below snippet to check if the directory exists:
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)
Code: Select all
<?php
if (($dir = realpath('/path/to/directory'/)) !== false) {
include ($dir . 'file.php');
} else {
die ('Directory is invalid!');
}
?>