Page 1 of 1

PHP includes reference wrong path - XAMPP on Windows 11

PostPosted: 21. April 2023 18:17
by stoneage
I have a new installation of XAMPP for Windows. When viewing a web page containing PHP include files, the path to the include directory that XAMPP is referencing is not correct.

I have projects in individual directories located in /xampp/htdocs/ (such as /xampp/htdocs/sitename/). I can view all elements of a web page at http://localhost/sitename/ as long as their are no includes.

Example of an include I'm using:
<?php include("_php-inc/myinclude.php"); ?>

Physical location of the include:
C:/xampp/htdocs/sitename/_php-inc/myinclude.php

However, XAMPP is looking here in the shared htdocs:
C:/xampp/htdocs/_php-inc/myinclude.php

-------------
Example error message on web page:
Warning: require_once(C:/xampp/htdocs/_php-inc/myinclude.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\sitename\index.php on line 2

Fatal error: Uncaught Error: Failed opening required 'C:/xampp/htdocs/_php-inc/myinclude.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\sitename\index.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\sitename\index.php on line 2
----------------

Do I need to define a path to each project's directory somewhere in Apache? I've searched this forum and the documentation, but must be missing something.
Thanks for any help!

Re: PHP includes reference wrong path - XAMPP on Windows 11

PostPosted: 21. April 2023 18:25
by Altrea
hi,

don't use paranthesis around include and require statements.
These are special language costructs not functions.

Re: PHP includes reference wrong path - XAMPP on Windows 11

PostPosted: 21. April 2023 21:16
by Nobbie
stoneage wrote:Example error message on web page:
Warning: require_once(C:/xampp/htdocs/_php-inc/myinclude.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\sitename\index.php on line 2


That error message does not match the PHP code you showed above. You showed an "include" statement, the error message is about require_once. You can also see, that your code calls require_once with a fully qualified path "C:/xampp/htdocs/_php-inc/myinclude.php", this is NOT the path you applied to the include, which was "_php-inc/myinclude.php". Its quite clear that you are doing something wrong, not PHP or Xampp.

Re: PHP includes reference wrong path - XAMPP on Windows 11

PostPosted: 21. April 2023 21:32
by Altrea
I tested it on my Testenvironment (Windows 11, XAMPP 8.2.4)

contents of \xampp\htdocs\sitename\index.php
Code: Select all
<?php include("_php-inc/myinclude.php"); ?>


contents of \xampp\htdocs\sitename\_php-inc\myinclude.php
Code: Select all
<?php echo 'Hello world'; ?>


Output of http://localhost/sitename/
Code: Select all
Hello world


Works like a charm.
Maybe there is something you didn't say us like a redirect fired by a .htaccess file or some of your provided information is not correct (which can happen easily by accident while obfuscating real data.