r/PHPhelp • u/Dr_philociraptor • Jul 26 '25
Basic php Help, Include Error, Pathing Issue?
Started coding in php , but I can't seem to use "include" or "require_once", really basic level stuff, syntax is correct because if I drop the file directly into the code it runs without issue, but if I try to pull a saved file I run into an error
code to call -
<?php include 'includes/footer1.php';?>code being called -
<footer> © <?php echo date('y')?></footer>
</body>
</html>Warning: include(includes/footer1.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\New folder\class_objects.php on line 46
Warning: include(): Failed opening 'includes/footer1.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\New folder\class_objects.php on line 46
both files are saved in my root folder, so what am I doing wrong here.
5
u/colshrapnel Jul 26 '25
That's a very interesting error!
First, it it introduces a very important topic of relative vs absolute paths which I recommend you to read.
Second, it's a very informative error message, that immediately explains the problem. Since it provides path to the current script, we can compare it with expected path. Here it goes:
is a folder where this file is called. Therefore, PHP looks for the included file in
because relative paths are just added up to the current path. While assuming
includesfolder stays in thehtdocs, the include path must beHence you get the error. Luckily, PHP has a variable that always contain a web-server root,
$_SERVER['DOCUMENT_ROOT']. So you can write your include paths like thisand it will be found from any file, wherever it placed.
2
u/[deleted] Jul 27 '25
Since this thread went so off the rails, I'm curious if the OP figured out the problem. It's really a simple thing. Also, don't use include, always use require_once. If you had warnings suppressed your program would have continued to execute and you'd never have even known that the include failed. Some advice:
I know you are working under windows, but don't create directories with special characters like spaces in them. That is just asking for trouble.
All PHP file based functions work with the file system. Often people confuse file system paths with paths in the context of a webserver. For things like css files, javascript or anything being referenced within HTML, those paths are relative to the web root.
For PHP, when you use a relative path with a file routine like include/require/fopen etc. relative paths are relative to the script that is doing the include. This can get confusing quickly when you are including scripts that in turn include other scripts. There are ways around this including the technique of generating fully qualified path constants using techniques like having something like this in a front controller of the project root. That can then be used to reliably include any other scripts relative to the application root directory, just using the relative path. For example:
define('APP_ROOT', dirname(__FILE__));So my guess for the OP is that they were confusing file system relative paths which use the current working directory of the script, and are not relative either to the entire filesystem or the webroot of the running webserver.
-5
u/[deleted] Jul 26 '25 edited Jul 26 '25
includes is a reserved word and shouldn't be used.
however if you have an includes sub directory in the www directory you do:
include './includes/footer1.php';
But the php file you are including is not correct, as it should be all php and not a mix of html and php
so inside footer1.php it should look like this:
Then on the page where you want it:
Because include does not load where you call include on the page. in a pure php page.
If you call it after ?> in the after HTML instance, it will be there where you put it. However, Its not an efficient way to use PHP