0

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?

green
  • 1
  • 1
  • 1
  • Tell us how Yours Script works! Do You have one "entry" index file witch serves all content, or stand-alone file for every "call"? – canni Aug 28 '10 at 10:08
  • @Dariusz Górecki forgive my ignorance what do you mean? – green Aug 28 '10 at 10:11
  • do You have index.php, witch include dependent classes/files and using them serves output ? or do You have one file per every page in Yours site – canni Aug 28 '10 at 10:20
  • yes I have an index.php page but my functions are in an includes folder with there own pages. – green Aug 28 '10 at 10:22
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:51

4 Answers4

0

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
  • 1,526
  • 11
  • 22
  • You kids take this `dirname(__FILE__)` thing as a kind of a magic wand that will solve any filesystem related problem. – Your Common Sense Aug 28 '10 at 10:11
  • 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 Aug 28 '10 at 10:20
  • There is now a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:51
0

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)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

Try this

require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');
Maulik Vora
  • 2,544
  • 5
  • 28
  • 48
0

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
  • 356,200
  • 43
  • 426
  • 500