PHP Includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

PHP Includes

Post by vchris »

I've recently setup php and mysql on my computer after formatting and I get this error when trying to include a leftnav.

Here's the code:

Code: Select all

<?php include("/includes/mf/leftnav.php"); ?>
Warning: main(/includes/mf/leftnav.php) [function.main]: failed to open stream: No such file or directory in c:\Inetpub\wwwroot\MySite\mf\index.php on line 13

Warning: main() [function.include]: Failed opening '/includes/mf/leftnav.php' for inclusion (include_path='.;C:\php5\pear') in c:\Inetpub\wwwroot\MySite\mf\index.php on line 13
I checked mutliple times the path and it is correct. I have tried this for the footer as well and I get the same error. I don't know if I'm doing anything wrong or maybe missing some permissions.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

by using a preceding slash (/) you're indicating that the includes folder is at the root of the site.

I'd be willing to bet (given the path of your file) that it's not. If you the includes folder is relative to the file calling it, then you need to use a relative path to it.
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

It is!
bike5
Forum Newbie
Posts: 6
Joined: Mon Jul 03, 2006 7:31 pm

Post by bike5 »

Try

Code: Select all

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/mf/leftnav.php"); ?>
You can do a

Code: Select all

print_r($_SERVER)
to find out what the important paths really are.
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

bike5 wrote:Try

Code: Select all

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/mf/leftnav.php"); ?>
You can do a

Code: Select all

print_r($_SERVER)
to find out what the important paths really are.
print_r($_SERVER):

Code: Select all

Array ( [ALLUSERSPROFILE] => C:\Documents and Settings\All Users [CLASSPATH] => .;C:\Program Files\QuickTime\QTSystem\QTJava.zip [CommonProgramFiles] => C:\Program Files\Common Files [COMPUTERNAME] => AGENTSMITH [ComSpec] => C:\WINDOWS\system32\cmd.exe [CONTENT_LENGTH] => 0 [FP_NO_HOST_CHECK] => NO [GATEWAY_INTERFACE] => CGI/1.1 [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5 [HTTP_CONNECTION] => keep-alive [HTTP_HOST] => localhost [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4 [HTTP_ACCEPT_ENCODING] => gzip,deflate [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7 [HTTP_KEEP_ALIVE] => 300 [HTTP_CACHE_CONTROL] => max-age=0 [HTTPS] => off [INSTANCE_ID] => 1 [LOCAL_ADDR] => 127.0.0.1 [NUMBER_OF_PROCESSORS] => 1 [OS] => Windows_NT [Path] => C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Panda Software\Panda Antivirus Platinum\;C:\Program Files\Common Files\Adobe\AGL;C:\Program Files\QuickTime\QTSystem\ [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH [PROCESSOR_ARCHITECTURE] => x86 [PROCESSOR_IDENTIFIER] => x86 Family 15 Model 2 Stepping 4, GenuineIntel [PROCESSOR_LEVEL] => 15 [PROCESSOR_REVISION] => 0204 [ProgramFiles] => C:\Program Files [QTJAVA] => C:\Program Files\QuickTime\QTSystem\QTJava.zip [REMOTE_ADDR] => 127.0.0.1 [REMOTE_HOST] => 127.0.0.1 [REQUEST_METHOD] => GET [SCRIPT_NAME] => /mf/index.php [SERVER_NAME] => localhost [SERVER_PORT] => 80 [SERVER_PORT_SECURE] => 0 [SERVER_PROTOCOL] => HTTP/1.1 [SERVER_SOFTWARE] => Microsoft-IIS/5.1 [SystemDrive] => C: [SystemRoot] => C:\WINDOWS [TEMP] => C:\WINDOWS\system32\config\SYSTEM~1\LOCALS~1\Temp [TMP] => C:\WINDOWS\system32\config\SYSTEM~1\LOCALS~1\Temp [USERPROFILE] => C:\Documents and Settings\LocalService [windir] => C:\WINDOWS [ORIG_PATH_TRANSLATED] => c:\inetpub\wwwroot\mysite\mf\index.php [ORIG_PATH_INFO] => /mf/index.php [ORIG_SCRIPT_NAME] => /mf/index.php [SCRIPT_FILENAME] => c:\inetpub\wwwroot\mysite\mf\index.php [PHP_SELF] => /mf/index.php [argv] => Array ( ) [argc] => 0 )
Leftnav include:

Code: Select all

Notice: Undefined index: DOCUMENT_ROOT in c:\Inetpub\wwwroot\MySite\mf\index.php on line 14

Warning: main(/includes/mf/leftnav.php) [function.main]: failed to open stream: No such file or directory in c:\Inetpub\wwwroot\MySite\mf\index.php on line 14

Warning: main() [function.include]: Failed opening '/includes/mf/leftnav.php' for inclusion (include_path='.;C:\php5\pear') in c:\Inetpub\wwwroot\MySite\mf\index.php on line 14
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

include('/includes/mf/leftnav.php') would be c:\includes\mf\leftnav.php; is this true? I would doubt it.
bike5
Forum Newbie
Posts: 6
Joined: Mon Jul 03, 2006 7:31 pm

Post by bike5 »

c:\inetpub\wwwroot\mysite\mf\index.php
It looks like wwwroot is the "webroot", not includes

not sure where ur includes are but I am guessing

Code: Select all

<?php include("/mysite/mf/includes/leftnav.php"); ?>
play around with the path you will get it soon!
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

bike5 wrote:c:\inetpub\wwwroot\mysite\mf\index.php
It looks like wwwroot is the "webroot", not includes

not sure where ur includes are but I am guessing

Code: Select all

<?php include("/mysite/mf/includes/leftnav.php"); ?>
play around with the path you will get it soon!
I changed the webroot folder to mysite so /includes/mf/leftnav.php is correct.
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Why not just remove the / in front of the includes/ folder ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

include 'includes/mf/leftnav.php';
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Give this a go, I'm guessing here:

Code: Select all

include '../includes/mf/leftnav.php';
vchris
Forum Contributor
Posts: 204
Joined: Tue Aug 30, 2005 7:53 pm
Location: Canada, Quebec

Post by vchris »

I want absolute paths not relative. It does work with my stylesheet but not for includes.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why would relative paths not work with your includes? This should not cause you a problem at all.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah wrote:Why would relative paths not work with your includes? This should not cause you a problem at all.
It may if he's using mod rewrite.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Would the error message show the actual path to the file that could not be included or the rewritten path?

I have never had a problem with declaring a variable in each page of my script called $page_root_path. I set that to path to the root directory and use that var for all of my includes. I have never had a problem with it.

Code: Select all

<?php
$page_root_path = './'; // This file is located in http://www.mysite.com/ root

$page_root_path = './../'; // This file is located in http://www.mysite.com/somefolder/

$page_root_path = './../../'; // This file is located in http://www.mysite.com/somefolder/someotherfolder/
?>
So regardless of where your pages are on your server, your files will always point to root and your includes will always include from there.

Code: Select all

<?php
include $page_root_path . 'includefolder/includefile.php';
?>
Post Reply