|Cross platform compatibility|
Differences between Mac and ISP
| 2:24 pm on Apr 12, 2008 (gmt 0)|
One of the difficulties in being new to an area (in my case PHP) is that it's hard to express what the problem is when you only half know what you're doing! I'm happy with C and Java, so I know what I'm trying to set out to do :-)
Here's my problem...
I want to include a file in a PHP source, and the file is in my home directory.
I pretty soon stumbled upon this, which nearly works....
<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>
Here's my problem...
On my ISP, the code picks up the path where my website is stored and adds it to '.'.
On the Mac, the code adds /Library/Webserver/Documents (instead of /~username) to '.'
But it's more curious still...
Take the case that the file I want to include is in my top level directory, and my PHP is two levels down, inside a folder in a folder...
On the ISP, if I include "filename", all is well. It goes up two levels and includes it.
On the Mac, it looks inside the current directory and fails.
If I include "/filename" instead, then my ISP fails - it goes to the top of the virtual server.
But it works on the Mac - the Mac adds ../.. to an include /filename (as I can demonstrate by trying to include "/absentfile" - it warns me that ../../absentfile is, well, absent.
What do I need to do to that code snippet to make it so that I can include a file in the home directory on both my Mac and my ISP with the same PHP?
| 8:54 pm on Apr 12, 2008 (gmt 0)|
If your relative paths are the same (1 directory below the document root, 2 directories below the document root, etc.) on your ISP as well as your local machine, your code would work using relative paths. If not, obviously it is going to fail.
I prefer just using the include/require based on an "includes" directory. Then I set the path to this directory in either Apache httpd.conf or an .htaccess file.
php_value include_path [php.net] "/path/to/includes:.:/path/to/PEAR"
If this is not an option, you will need to either add some logic to determine your include based on your IP, operating system, etc. or you will need to structure your local setup much like your ISP setup.
| 8:32 am on Apr 13, 2008 (gmt 0)|
I've gone for relative paths - I'll need to remind myself to edit the page manually if I ever move it in the hierarchy of folders, but that apart it's definitely the easiest option.
I appreciate you spending time on what's almost certainly a laughably trivial question from your point of view :-)
| 7:10 pm on Apr 13, 2008 (gmt 0)|
No, I don't feel that is a trivial question at all. A common question, yes, but understandably so as with flexibility often comes some level of complexity. PHP includes are a large part of making life easy on the server side and understanding the details of how they work will be worth your time and effort.
Here is a thread from our PHP Forum Library [webmasterworld.com] regarding includes that may be handy:
Control your included files [webmasterworld.com]