Jump to content

Weird PHP Include Problem (changes between Linux and Windows)


Kryptix

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:

 

C:\Server\Apache\htdocs\RSCEmulation

 

On Linux the file location is like this:

 

/home/rscemulation/public_html

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.