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 trialRyan Hellerud
3,635 Pointsphp 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
Micah Kline
17,831 PointsIt sounds like you looking for the inc folder in your current folder try going down one folder using "../"
include('../inc/header.php');
Rebecca Trynes
7,271 PointsI'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
3,635 Pointsi actually need to go down 2 folders but i thought '/inc/header.php' would just look from the root
Micah Kline
17,831 PointsIt works relative to your current folder.
Ryan Hellerud
3,635 Pointshow do i go down 2 levels?
Micah Kline
17,831 PointsTry using Root Relative Includes
// ../ = one folder down
// ../../ = two folders
// ect.
<?php
$myRoot = $_SERVER["DOCUMENT_ROOT"];
// echo $myRoot
include($myRoot . '/inc/folder/fileToInclude');
?>
Ryan Hellerud
3,635 Pointsthanks
Andrew Shook
31,709 PointsRyan, 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.
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsThink 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
3,635 Pointsim not using wordpress im using foundation 5 with some minor php
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsYou 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.
Micah Kline
17,831 PointsMicah Kline
17,831 PointsAlso try using root relative links instead of relative links