3

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
  • 3,100
  • 5
  • 29
  • 36
  • What do you mean working from "file" and "not from server"? Command line instead of Apache? – zneak Mar 13 '10 at 19:25
  • Yes @zneak. The PHP files are just processed from the file by PHP, and then the output html file is rendered in a browser. The request doesn't go to a server, request a page, and then return the rendered html. So theres no question of htaccess or apache config here. – Neil Mar 16 '10 at 19:16

4 Answers4

2

You can try this method.

require_once(dirname(__FILE__) . '/example.php');
Can Aydoğan
  • 1,040
  • 2
  • 10
  • 23
0

Try

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

in a shared include file

or edit your php.ini and change the include_path variable (I'm using MAMP so can't tell you the exact path to php.ini)

Steve
  • 3,601
  • 4
  • 34
  • 41
0

It truly is an include_path issue. The default seems to be .:/usr/lib/php; perhaps if you launched the PHP command from the directory in which are your scripts, it would be fine?

zneak
  • 134,922
  • 42
  • 253
  • 328
0

this works every time for me:

<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/common/header.php'); ?>

Matt Ryan
  • 1,717
  • 2
  • 20
  • 30
  • 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. – Neil Apr 23 '11 at 06:39