What is the right path for PHP includes on a Mac?

10,476

You can try this method.

require_once(dirname(__FILE__) . '/example.php');
Share:
10,476
Neil
Author by

Neil

Moo

Updated on June 05, 2022

Comments

  • Neil
    Neil almost 2 years

    Running Mac OS X 10.5.8, with PHP 5.2.11 Pre-installed. Using Coda 1.6.10.

    I'm writing PHP files, and then preview them running from file, not server. This was working fine till I tried PHP includes. These don't work as a relative path, only as an absolute from the root of the drive.

    Is there any way I can use statements like

    include_once "common/header.php";

    without specifying my entire file path like so :

    include_once "/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0/common/base.php";

    ,where ColoredLists_v1.0 is the directory with all the website files in it. I tried solutions like prepending _SERVER[DOCUMENT_ROOT] or dirname(File) to the file paths, but that didn't work as the variables were not set. Is there any easy way to do this, or a configuration I can change so that it looks in a specific directory by default instead of looking at the drive root? Currently, echo_include_path shows .:

    When I include this line at the start of the script, it works:

    set_include_path('/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0');

    However, if I want to do this for all my scripts, I can't seem to make the change permanent. Even after I edited the Unix include_path in my php.ini, it doesn't seem to work.

    UPDATE: I finally decided that the easiest sureshot way was to run the Apache server locally. It comes preinstalled with Mac, so didnt face much trouble there. I modified the httpd.conf to only allow connections from localhost, thus alleviating my security fears. Ofcourse, this still opens up port 80, but I'm behind a router, so I'm gonna hope I'm not very exposed. And Coda includes a great features in 'Sites', where I can set the local url, so it works brilliantly now. Only downside is, now, after code edits, I need to manually refresh the preview.

  • Neil
    Neil about 13 years
    I don't think any of the $_SERVER variables will work, considering its running PHP off of commandline, not a server. I dunno. I'll still try it out later.