I've been putting some include stuff into my sites for navigation lately. The code is working fine for all of the folders in the root directory. The subfolders and lower levels are not working properly. I've been adding this to the pages <?php include ('../include/navigation.php'); ?> HTML: I have also tried this in hopes of success <?php include ('http://www.myurlhere.com/include/navigation.php'); ?> HTML: Here are the warnings I am getting Warning: include(../include/navigation.php) [function.include]: failed to open stream: No such file or directory in /home/content/03/4610003/html/games/texas-holdem/index.php on line 32 Warning: include() [function.include]: Failed opening '../include/navigation.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/03/4610003/html/games/texas-holdem/index.php on line 32 HTML: Would appreciate any help with this.
The 2 URLs below work fine http://www.mydomain.com/index.php http://www.mydomain.com/directory1/index.php The folder below returns the warnings http://www.mydomain.com/directory2/folder/index.php
there's a solution but not the best one out there. You can try with absolute paths also ie. <?php include('/home/username/public_html/includes/file.php'); ?> Code (markup): where ofcourse you should replace the path above with the real path.
you can use full paths like suggested above or <?php include ('../../include/navigation.php'); ?> In the second example (http://www.mydomain.com/directory2/folder/index.php)
For the record, using hardcoded full paths is a bad idea, it makes life hell if/when you want to move your site to a new server. The solution you chose is the right thing. Well, the real right thing is to dynamically calculate your script path, or have a setup script that calculates it and saves it in a global include. But this is good enough.
Eh, I like full paths.. Just define a constant that sets up the location: define('DOCUMENT_ROOT', '/home/username/public_html/'); Then include as follows: include DOCUMENT_ROOT . 'includes/bla.php';
Better, define('DOCUMENT_ROOT', str_replace('\\', '/', dirname(dirname(__FILE__)))); include DOCUMENT_ROOT . '/includes/something.php'; PHP:
Yeah, that's good if you're distributing a script and don't know the exact location, but for my own sites I always try to keep it static. No reason to call dirname if I already know the location
Ehh, it's just a little optimization. I do what you do during an installation script and then write it to a config file. Less function calls and all that..