Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting include_path on hosted server 1

Status
Not open for further replies.

Nehurd

Technical User
Jan 26, 2005
5
US
Hi,

I'm just learning php and am having trouble using includes with files that are not in the current directory. Here's the error message I get:

Warning: main(header.html): failed to open stream: No such file or directory in /home/content/r/m/o/rmosenkis/html/layout-top.php on line 16

Warning: main(header.html): failed to open stream: No such file or directory in /home/content/r/m/o/rmosenkis/html/layout-top.php on line 16

Fatal error: main(): Failed opening required 'header.html' (include_path='.:/usr/local/lib/php') in /home/content/r/m/o/rmosenkis/html/layout-top.php on line 16

I have created an /includes directory off the root that I would like to use for all included files. I need help setting the default include path.

Thanks in advance for the help!
 
Hi,

I suppose that you can not edit php.ini file, to set include_path parameter.

In that case you can use:
Code:
<?php
$old_inc_path = ini_get('include_path');
ini_set('include_path', $old_inc_path.':/your/inc_dir_here');
// the
// rest
// of
// the 
// script

?>

at the begining of your script

___
____
 
Do I need to include this in every file that has an include? Is there any way to do this globally? (You are right -- I don't think I have access to php.ini, though I do have an .htaccess file that I can modify.
 
ya,
you have to include it in all your `main' scripts (the script that you want to show in the browser).

to do that:
- first create a small script set_inc_path.php with the code froum previous post
- in every script do: include './set_inc_path.php'; ,
this if the set_inc_path.php is in the same directory,
other way you have to call imclude './relative/path/set_inc_path.php'


thats all


___
____
 
I'm still having problems:

I have my includes in a folder off the root called "includes" (without the quotes).

Here's the script that I added to the top of my file right after <body>:

<?php
$old_inc_path = ini_get('include_path');
ini_set('include_path', $old_inc_path.':/includes');
?>

Later in this file, I want to include a file that's in the /includes directory. I use this script:
<?php require ("/header.html"); ?>

When I run the page, I get this error:

Warning: main(/header.html): failed to open stream: No such file or directory in /home/content/r/m/o/rmosenkis2/html/layout-top.php on line 20

Warning: main(/header.html): failed to open stream: No such file or directory in /home/content/r/m/o/rmosenkis2/html/layout-top.php on line 20

Fatal error: main(): Failed opening required '/header.html' (include_path='.:/usr/local/lib/php:/includes') in /home/content/r/m/o/rmosenkis2/html/layout-top.php on line 20

Can you tell what I'm doing wrong?

Thanks!
 
Your problem is your use of pathnames.

You have to remember that PHP's filesystem functions can operate on the server's entire filesystem and are not constrained to the document root.

When your script performs:

require ('[red]/[/red]header.html');

PHP is going to try to include a file that resides in the topmost diretory of the filesystem, not in the topmost directory of your virtual server's document root.

Try:

require ('header.html');



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Path mis-usage aside (listen to sleipnir on that), if your host is running apache and allows .htaccess usage, you can put a line like this in there:
Code:
php_value include_path ".:/home/mydir/include:/usr/share/pear"
 
Per the suggestion, I just added this line to the end of my htaccess file:

php_value include_path ".:/home/content/r/m/o/rmosenkis2/html/includes"

It crashed my site (gave me an Internal Server Error). Any thoughts?
 
Per the suggestion, I just added this line to the end of my htaccess file:

php_value include_path ".:/home/content/r/m/o/rmosenkis2/html/includes"

It crashed my site (gave me an Internal Server Error). Any thoughts?
 
I'm not sure what to tell you.

On my system, I have a website the document root of which is the directory /home/sites/test/html. In that directory I have an includes directory (the complete path to the includes directory is /home/sites/test/html/includes).

In the includes directory I have a file foo.inc, which reads:

Code:
<?php
print "foo";
?>

I have two versions of test_include.php:

Code:
<?php
ini_set('include_path', ini_get('include_path') .':/home/sites/test/html/includes');

include ('foo.inc');
?>

and

Code:
<?php
include ('includes/foo.inc');
?>

Both versions of test_include.php output a page consisting entirely of:

foo



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top