Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ryan Hellerud
Ryan Hellerud
3,635 Points

php include files not working

So i had to add some crap and i had a php header and footer included in my page. The directory structure was mainroot/branchx/filex.php . Now, I had to add another dir in the brachx folder so it now looks like mainroot/branchx/branchy/filex.php but now my php includes dont work. I've tried <?php include ('/inc/header.php') ?> which should still point to the same dir, and also <?php include ('../inc/header.php') ?> but it's giving me errors: Warning: include(/inc/header.php): failed to open stream:

No such file or directory in C:\Users\rhellerud\Documents\ofl\courses\ofl\electives.php on line 1

Warning: include(): Failed opening '/inc/header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\Users\rhellerud\Documents\ofl\courses\ofl\electives.php on line 1

anyone know what could be going on?

7 Answers

It sounds like you looking for the inc folder in your current folder try going down one folder using "../"

include('../inc/header.php');

Also try using root relative links instead of relative links

<?php

$myRoot = $_SERVER["DOCUMENT_ROOT"];
// echo $myRoot
include($myRoot . '/inc/folder/fileToInclude');
?>
Rebecca Trynes
Rebecca Trynes
7,271 Points

I'm no expert, but you could try setting up a reference to your root folder in a config.php file:

define("ROOT", __DIR__ ."/");

and then use:

include(ROOT . 'branchx/branchy/filex.php')

whenever you need to call on an include file, and don't forget to:

require_once 'config.php';

in all of your relevant pages.

Ryan Hellerud
Ryan Hellerud
3,635 Points

i actually need to go down 2 folders but i thought '/inc/header.php' would just look from the root

It works relative to your current folder.

Ryan Hellerud
Ryan Hellerud
3,635 Points

how do i go down 2 levels?

Try using Root Relative Includes

// ../ = one folder down
// ../../ = two folders
// ect.

<?php

$myRoot = $_SERVER["DOCUMENT_ROOT"];
// echo $myRoot
include($myRoot . '/inc/folder/fileToInclude');
?>
Andrew Shook
Andrew Shook
31,709 Points

Ryan, if i'm not mistaken you are working on a pc, which uses \ instead of / to separate directories. PHP has a build in constant for that will select the correct directory separator based on the operating system. You really should use that.

Think no is telling you but include is more native to php. To become a better wordpress developer you need to use the wordpress native stuff. in your cause it would be get_template_part('inc/header'); .This is because if not you stop some wordpress code from functioning as you would expect them to and why do the heavy lifting when wordpress can do it all for you?

Ryan Hellerud
Ryan Hellerud
3,635 Points

im not using wordpress im using foundation 5 with some minor php

You need to correct your tags. In respect to tyga I would leave it to the php experts I can write php in wordpress but believe it or not I get scared with just php on its own.