Christoph Hörterer wrote:
> Justin Koivisto wrote:
>
>> Christoph Hörterer wrote:
>>
>>> But when a file tries to php include() some file that is referred to
>>> like /include/file.txt (include("/include/file.txt")
I get errors:
>>>
>>>> Warning: main(/include/file.txt): failed to open stream: No such
>>>> file or directory in /Users/pofi/Sites/pofi.de/index.html on line 18
>>>>
>>>> Warning: main(): Failed opening '/include/file.txt' for inclusion
>>>> (include_path='.:/usr/lib/php') in
>>>> /Users/pofi/Sites/pofi.de/index.html on line 18
>>>>
>>> Is there another directive to enable these paths? Or do I have to
>>> tweak the php.ini somewhere? Or did I make a mistake in php syntax?
>>
>> easiest way...
>>
>> include dirname(__FILE__).'/include/file.txt';
>>
>> Where "/include/file.txt" is relative to the file that you are working
>> on.
>
> Ok,
> didn't really understand your answer but it brought me to something
> obvious. Thank you. As virtual hosts already work, I just add the
> virtual domain:
>
> include("http://test.pofi.de/include/file.txt");
>
> So it works, even in subdirectories.
> How come that php doesn't accept the root directory of a virtual host,
> while html and css does?
Because PHP uses the SYSTEM path, not the HTTP path. For instance, using
"http://test.pofi.de/include/file.php" will actually open the file via a
GET request to the server. Therefore, if you have function definitions
in that file, you won't have the function available for use in other files.
Quote from the PHP manual about include:
<quote>
If "URL fopen wrappers" are enabled in PHP (which they are in the
default configuration), you can specify the file to be included using an
URL (via HTTP or other supported wrapper - see Appendix I for a list of
protocols) instead of a local pathname. If the target server interprets
the target file as PHP code, variables may be passed to the included
file using an URL request string as used with HTTP GET. This is not
strictly speaking the same thing as including the file and having it
inherit the parent file's variable scope; the script is actually being
run on the remote server and the result is then being included into the
local script.
</quote>
In the example above, the file would actually be located at:
/Users/pofi/Sites/pofi.de/include/file.php
That is what PHP is expecting for a local pathname. Using
"dirname(__FILE__)" will return the "/Users/pofi/Sites/pofi.de" portion
for you if it is in a file in the DOCUMENT_ROOT. Doing the same in the
file.php would yield "/Users/pofi/Sites/pofi.de/include"
Also note that you can set your include_path that PHP uses to allow you
to simply do:
include "file.php"
In .htaccess or the VirtualHost directive, simply add a line like:
php_value include_path "/Users/pofi/Sites/pofi.de/include"
You could also do something similar in a PHP script:
$inc_path=ini_get('include_path');
ini_set('include_path',$path.':'.$_SERVER['DOCUMENT_ROOT'].'/include');
The thing to remember about css, javascript, html is that they are not
executed server-side, PHP is. That is why PHP expects a local file path
rather than a URI.
--
Justin Koivisto - spam.DeleteThis@koivi.com
<a style='text-decoration: underline;' href="http://www.koivi.com" target="_blank">http://www.koivi.com</a><!-- ~MESSAGE_AFTER~ -->
>> Stay informed about: virtual hosts and root directory for php