Forum Moderators: coopster
Here's the general problem:
So long as I keep all my files in the same directory as the include files they call, everything works.
But I have multiple, nested directories. None of my include statements work in files in any directories above the root (same directory as the calling file). They all return "failed to open stream: No such file or directory" errors.
I know this is standard, so I'm obviously just missing something basic. Is there some particular way of writing out that path in the include statement, perhaps? Relative and absolute paths aren't working...
Or is it rather something I need to set up on the server itself, in a config file of some sort? If so, what file, where, and what gets changed? ;)
If it's a server setting, then I've got another issue: I'm building and testing on my server, but need to deliver the files to a client for hosting on different server/domain. I really don't want to have update every include statement... see my confusion?
Any explanations?
I'm very new to php (as in, "tonight" new), so be gentle and assume nothing.
$_SERVER[DOCUMENT_ROOT].'/your_include_folder_path/
you can also use ini_set function to change php.ini settings to match the include path of the configuration with your include path for your scripts' lifetime, please read manual for ini_set i.e php.net/ini_set
absolute path like [domain.com...]
will never work you need to attache your php files with your local server addresses, to find the exact path of a file you can use
echo real_path('config.php/anyother page name in the folder');
this will display the exact path of your file and you can then use that path to include files.
if you are using .htaccess for redirection etc then you may need to be very careful with your paths structure because in that case the URLs are translated according to .htaccess rewrite rules and you have to follow those rules to point to a file.
i hope i answer your doubts..
So maybe this helps explain my situation more:
On the root directory, I have index.php. This has a call to include "shared.inc" - e.g.:
* root directory = www.mysite.com/
* php page file = www.mysite.com/index.php
* include file created and put in the root directory = www.mysite.com/inc/file.inc
* include statement used inside "www.mysite.com/index.php" is: <?php include 'file.inc';?>
That works just fine. So next "test" -
I can also create a subdirectory on the root, then put the includes there (instead of on the root itself) and the include statement still works, e.g.:
* root directory = www.mysite.com/
* same php page file = www.mysite.com/index.php
* new include folder = www.mysite.com/inc/
* another include file created and put in that new folder = www.mysite.com/inc/file2.inc
* include statement used inside "www.mysite.com/index.php" to call 'file2.inc' is: <?php include 'inc/file2.inc';?>
This also works fine. So my assumption here (not knowing much about how php is configured on the server), is that my root directory is the include path?
But then what confuses me is I create a new file in a further nested directory and try to again use an include statement, calling an included file that resides on the root - e.g.:
* root directory = www.mysite.com/
* new, nested directory = www.mysite.com/some/directory/
* new php page file = www.mysite.com/some/directory/newfile.php
* original include file, still located on the root directory = www.mysite.com/inc/file.inc
* include statement used inside "www.mysite.com/some/directory/newfile.php" is: <?php include 'file.inc';?>
And this is where it breaks - I get that error. Basically, for any file that I try to use an include statement in, if that file doesn't exist in the root directory, the include statement doesn't work.
I don't know if I've made my problem any clearer? In a nutshell, I don't know how to find out what the actual include path is on either my server space (shared, hosted on Dreamhost - defaulted to PHP-CGI, not Apache, if that helps) OR on the client's server (no idea how it is setup at this time).
So if I use either of the methods you described - $_SERVER[DOCUMENT_ROOT].'/your_include_folder_path/ or using the ini_set function - does that rely on how the servers are set up to begin with?
Sorry for such wide open questions, but I appreciate any further info, advice. My host is unavailable for this kind of matter, and the client doesn't know either so until I can get someone from their host, I'm on my own. Suggestions?
--- is *meant* to point to the original include file residing on the root directory. So the include file is there. And what I presume is the problem is that my method of pointing to that include file is incorrect...
Thanks for any suggestions.
Using "../" however got me back down the directory. And now all seems fine.
I'm still curious about how to set up these php settings... but for now, I'm just back to work!