views:

37

answers:

2

Good evening. I found a few questions that seemed to ask the same thing I am asking, but none of the answers seems to work for me. Basically, I have a few generic PHP files (mostly classes, but some are not) that I want to be usuable by multiple PHP sites.

Here is an example of my folder structure:

Site A Root C:\Personal\WebSites\SiteA

Site B Root C:\Personal\WebSites\SiteB

Shared Code C:\Personal\WebSites\Shared

From Site A I tried using include("../Shared/foo.php") to reach the files, but this did not work. I have a feeling this is due to the folder being beyond the site root folder of SiteA. Is there a way to simply include files from the Shared directory? I can't imagine this is an uncommon practice, but I just can't figure it out.

BTW, I am using PHP 5.2.14 through IIS (via FastCGI) on Windows 7 if that matters. Eventually, I will be moving these sites to Linux/Apache hosting, so I would prefer any solution to work with either.

Thanks!!

+2  A: 

Your best bet is to setup a directory anywhere you wish (let's say C:\Personal\Shared), and edit your php.ini include_path to add this directory as an include path. Make sure you set permissions correctly so IIS allows you to access this directory (I'm not 100% on how to do that with Windows, but it should be pretty easy).

Then all you need to do is include the file as if it was in the same directory (PHP handles searching include directories for you).

For example if you had C:\Personal\Shared\foo.php you can include it like this:

include("foo.php");
Sam Day
@Sam, great answer, but meder just barely beat you to it. Thanks!
Jason Watkins
+2  A: 

In your php.ini you can add directories to the include_path, and them include files from then.

"C:\Personal\Websites\Shared;"

If you append a string like that to the already existing include_path you can do include 'specialDir\Class.php' wherespecialDiris a directory inShared`.

meder
@meder, this is perfect. Worked like a charm! Thanks!
Jason Watkins