views:

39

answers:

4

I get the following error in one of my function includes.

require(../htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory

I can correct this problem if I code the path

./htmlpurifier/library/HTMLPurifier.auto.php

But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function include every time which defeats the purpose of my include. Is there a way I can have this work with out having to re-code the path every time? Is there a way to include the full path?

A: 

Try this:

require realpath(dirname(__FILE__) . '/htmlpurifier/library/HTMLPurifier.auto.php');

That way you always include files relative to the path of the current file.

Robert Ros
You kids take this `dirname(__FILE__)` thing as a kind of a magic wand that will solve any filesystem related problem.
Col. Shrapnel
@Robert Ros I get this error with your code `Fatal error: require() [function.require]: Failed opening required '' (include_path='C:\wamp\www\New Project\htmlpurifier\library;.;C:\php5\pear') in C:\wamp\www\New Project\includes\categories.php on line 7`
green
A: 

You are using wrong path.
Just put these 2 lines together and feel the difference.

../htmlpurifier/library/HTMLPurifier.auto.php
./htmlpurifier/library/HTMLPurifier.auto.php

That's 2 different paths and will never work at the same time. Latter one points to the current directory while first one - one level higher.

But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function

that's sensible desire. Here you go:

require $_SERVER['DOCUMENT_ROOT'].'/htmlpurifier/library/HTMLPurifier.auto.php';

(assuming this htmlpurifier folder lays in the document root)

Col. Shrapnel
i get this error now with your code. `require(C:/wamp/www//htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory`
green
How about posting the path to HTMLPurifier.auto.php?
captaintokyo
@green **assuming this htmlpurifier folder lays in the document root** otherwise you have to set another path here.
Col. Shrapnel
my path to my htmlpurifier folder is `http://localhost/New%20Folder/htmlpurifier/`
green
@green well try to use `'/New Folder/htmlpurifier/library/HTMLPurifier.auto.php'` then in the expression I gave you.
Col. Shrapnel
@Col. Shrapnel nothing :(
green
A: 

Try this

require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');
Maulik Vora
A: 

How about configuring the path to this .php file as a configuration variable in your script? Presumably you'll be loading some common functions library in all your pages, so just do:

$PATH_TO_PURIFIER = 'C:\this\that\whatever\HTMLPurifier.auto.php';

in that common file, then you can just do

include('commonstuff.php');
include($PATH_TO_PURIFIER);

As long as commonstuff.php is somewhere in your PHP's include_path, it's far more reliable to do it this way rather than try to dynamically calculate the path everywhere else. Especially if your directory structure might change, rendering the calculated path invalid and forcing you to change every PHP file yet again.

Marc B