Jump to content

Weird PHP Include Problem (changes between Linux and Windows)


Recommended Posts

I use my desktop (Windows) to make websites and then once it's completed transfer to my Linux server but I've come across a weird issue.


On Windows the file location is like this:




On Linux the file location is like this:




Now in those folders I have a folder called 'include', in that I have 2 files: header.php, functions.php


In the base folder I have index.php. I then include header.php by using: include "include/header.php";


Then I include functions.php (from header.php) by using: include "functions.php";


Now on Linux this chucks an error, even with "./functions.php" -- I have to use include "include/functions.php" even though I'm already in that directory from the file I'm calling it from, although admittedly the file is being included from the directory below but this works fine on Windows with a base PHP install.


This Linux box was given to me and I'm not sure if any PHP settings have been changed or not.


I also have a config.php that I keep below the public directory (htdocs or public_html) but it won't let me include it on the Linux server by using "../../config.php" (I've tried all levels ranging from 0-5 with no luck)


Can anyone shed some light on this please? I need it to work the same both on the Windows and Linux boxes as I copy and paste the files regularly and it's annoying changing them!


Cheers. :)

Link to comment
Share on other sites

The correct path is "include/functions.php".


include() et al. work according to the current working directory. That's generally the location of the first PHP script that started running, and generally does not change according to the currently-executing script.

You started in / so unless you say otherwise all paths are relative to that, regardless of most anything else.


There are a couple explanations for why it worked that way on Windows. Most likely is that PHP was set up with a different set of include paths (which include() will search through when looking for files).


The most reliable way to include files is to use absolute paths. Your choices are:

// only when running from a web server (Apache, IIS, etc)
include $_SERVER["DOCUMENT_ROOT"] . "/path/to/file.php";

// only in PHP 5.3+
include __DIR__ . "/path/to/file.php";

// everywhere
include dirname(__FILE__) . "/path/to/file.php";

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.