NYCPHP Meetup

[nycphp-talk] apache2/php5 and include ( ) paths

Dan Cech dcech at phpwerx.net
Thu Jun 29 14:36:21 EDT 2006


Mikhail Ledvich wrote:
> Jon,
> You're completely right. When running  print_r($_SERVER) from the 
> localhost browser I get
> $_SERVER["DOCUMENT_ROOT"] = C:/AppServ/www
> however, when running it from command line it's empty.
> 
> Perhaps excuse the naive question, but how do I set C:/AppServ/www as 
> the Document root for when I run files from the command line?

If you need to be able to access include files, you have 2 options:

1. set the include paths dynamically
2. change to the directory of the script

in both cases, you'll find the construct:

basename(__FILE__);

very handy, eg:

directory of your script: basename(__FILE__);
parent directory: basename(basename(__FILE__));
parent of that directory: basename(basename(basename(__FILE__)));
etc...

then you can either do something along the lines of:

ini_set('include_path',basename(__FILE__));

or:

chdir(basename(__FILE__));

at the top of the script you are executing from the command line.

Dan

> Thank you again,
> Mikhail
> 
> 
> ------------------------------
>> Message: 3
>> Date: Thu, 29 Jun 2006 07:10:50 -0400
>> From: Jon Baer <jonbaer at jonbaer.com>
>> Subject: Re: [nycphp-talk] apache2/php5 and include ( ) paths
>> To: NYPHP Talk <talk at lists.nyphp.org>
>> Message-ID: <C2D4040E-0D55-43EE-B2B1-23A0A8CB5359 at jonbaer.com>
>> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
>>
>> What happens when you add the -C flag to the mix?  From what I recall  
>> on windows you path is actually set  where your interpreter is setup  
>> (ie c:/php5/php.exe) so your 'document root' in that case is really  
>> c:/php5.  I could be wrong, trying running a print_r($_SERVER) from  
>> your CLI program to see what differences you get.
>>
>> Are you using PHP via Cygwin?
>>
>> - Jon
>>
>> On Jun 28, 2006, at 10:46 PM, Mikhail Ledvich wrote:
>>
>>   
>>> Hi all,
>>>   Perhaps someone can help me out with a weird error I am  
>>> experiencing.
>>> I've installed Apache2, PHP5, MySQL5 on a WindowsXP machine and
>>> everything works like a charm. Except when I try to use php.exe from
>>> command line. If I execute a php file from command prompt using the
>>> php.exe, if the include/require paths are not entirely hardcoded
>>> (c:\AppServ\www ...) but are dynamically set (../../config.php): it
>>> throws errors.
>>>
>>> Warning: include_once(../../config.php): failed to open stream: No  
>>> such
>>> file or directory in c:\AppServ\www\fill_combination.php on line 10
>>>
>>> However, if I put the same into the browser, it works, but from local
>>> command line I get path errors. Any ideas? I am at the end of my wits.
>>>
>>> Thanks,
>>> mikhail



More information about the talk mailing list