Jump to content

include can't find the page to include


PHPnewb_JavaAdept

Recommended Posts

Hi,

 

I'm not a PHPguy. I'm a corporate java guy.  But I'm helping a friend who recently moved their PHP-based website from one host to another.  I'm having a heck of a time with it.

 

Here is the file structure:

 

|-- page.php

|-- cartform.php

|-- folderA

        |- content.php

 

Inside /page.php, there is a statement like:

 

include ('/folderA/content.php');

 

and then inside /folder/content.php, there's a line that says:

 

include ('../cartform.php');

 

These don't work.  What I've found *does* work is the following:

 

inside /page.php:

  include ('./folderA/content.php');

 

inside /folderA/content.php:

  include ('./cartform.php');

 

I have two observations:

1) it makes no sense to me, that I should need to say ./ in the first case

2) it makes even less sense to me, that ./ should work in the second case

 

Can someone help me figure this out? 

 

Oh, the error message is like this:

 

Warning: include(../cartform.php) [function.include]: failed to open stream: No such file or directory in /home/content/t/h/i/domainname/html/folderA/content.php on line 52

 

Warning: include() [function.include]: Failed opening '../cartform.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/t/h/i/domainname/html/folderA/content.php on line 52

 

Link to comment
https://forums.phpfreaks.com/topic/134423-include-cant-find-the-page-to-include/
Share on other sites

When you start file paths with a / PHP will load the file from the root of the file sytem not your websites document root folder.

 

The reason why ./ works is because it tells PHP to load the file from the current working directory.

 

To ensure PHP always loads the file from your sites document root prepends your paths with $_SERVER['DOCUMENT_ROOT'] eg

include $_SERVER['DOCUMENT_ROOT'] . '/folderA/content.php';

Thanks for your reply wildteen88.  I have one follow-up question, and one thing I'm still stuck on.

 

1) why would this have worked on the previous host?  Is it maybe something that they could have configured, in php.ini (or elsewhere)?  I think the previous host was pretty small-time.  Maybe "/" on that host was actually "/" on the server??  (wild!)

 

2) Your explanation makes sense to me, but the behaviour doesn't seem consistent.  If "/" means absolute, from filesystem root, then why doesn't "no slash" mean "from current directory" ?  Said another way: why wouldn't "../cartform.php" work, from the file located at <webroot>/folderA/content.php?

 

 

1) why would this have worked on the previous host?  Is it maybe something that they could have configured, in php.ini (or elsewhere)?  I think the previous host was pretty small-time.  Maybe "/" on that host was actually "/" on the server??  (wild!)

Not sure about that. All I can suggest is for you to not start your file paths with a /. Unless you're specifying an absolute path.

 

2) Your explanation makes sense to me, but the behaviour doesn't seem consistent.  If "/" means absolute, from filesystem root, then why doesn't "no slash" mean "from current directory" ?  Said another way: why wouldn't "../cartform.php" work, from the file located at <webroot>/folderA/content.php?

When using includes, all paths should be relative to the parent script (in your case page.php).

 

This is why I always prepend $_SERVER['DOCUMENT_ROOT'] to my includes. This is how I'd do it:

page.php

define('ROOT', $_SERVER['DOCUMENT_ROOT']);

include ROOT . '/folderA/content.php';

 

folderA/content.php

// NOTE: The constant ROOT is inherited from page.php

include ROOT . 'cartform.php';

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.