Defined directory works on 1 server but not the other


I decided I was going to play with defining a constant and then using the constant to include almost every file so that the folder could be placed anywhere (preferably outside public_html) I am defining the directory as a constant Like:

if(!defined(‘DIR’)){define('DIR, dirname(FILE));}

and when I call a file it is like so


So I spent the last two weeks completely rewriting the script on my server while updating it and it works GREAT!!! The problem however is migrating from server to server. On my home server the the back slashes “\” work just like they should. On Server 2 the constant (DIR) changes the backslashes to forward slashes “/” and throws an error. I end up with something like


So i did what any smart man would do (i thought)

First I used string replace before defining the constant and changed the slashes. Still errors.

So i get smarter and use find/replace to go through and change all of my hard coded backslashes to forward slashes. Now the main page loads but when I make a selection I get an error again. I double and triple checked the error displayed, the correct file is being queried but the server says it is not there or failed to include.


any suggestions or criticism? I am a nub

instead of using ‘\’ use ‘/’ always .

on windows it works both ways but nix throws error.

i didnt realize that “\” only works on windows ty for that

but as I stated above I changed the “\” to “/” and the first page loads but when a function calls a function then an include it doesn’t work any more

This isn’t really my area, but if you are using includes, perhaps you want something like this:

<?php include $_SERVER["DOCUMENT_ROOT"] . "/dir-folder/the-folder/file.php"; ?>

Or, you could use the predefined PHP constant DIRECTORY_SEPARATOR. :wink:

Anthony I am guessing you are suggesting using


The big question is why would the main page load and not the rest?

It really should not matter if you have forward or back slashes. PHP will usually compensate. You might have something else going on that obscures the problem.
Make a simple file called test.php in your public_html with something like:

#include '/home/username\\public_html\\folder\\file.php';

And see if it works on Server 2.
And in most cases you will want to use DIR instead of dirname(FILE)

Thats what I thought, defining the directory should have solved my problems. I was wrong.

My Server will not include a file that isn’t written properly.

IE /home/user/public_html/folder\folder2\file.php will not work.

Error reporting is on thats how I am positive it is calling the right directory.
As I stated above I get the index page to load properly after changing the slashes. But now when I select a link it says the file is not there.

IE: (an actual error statement)

Warning: include(/home/user/public_html/folder/folder2/file.php) [function.include]: failed to open stream: No such file or directory in /home/user/public_html/folder/folder3/file.php on line 21

maybe a server setting? i seriously just dont get it

First off, did you do as suggested and make a simple test file with just an include statement in it?

This is confusing:

As I stated above I get the index page to load properly after changing the slashes. But now when I select a link it says the file is not there.

What exactly do you mean by index page? usually an index page is just something like index.php stored in public_html. So there should be no slashes involved.

And what exactly do you mean by “select a link”? Are you generating <a href=“”> elements and trying to load a file based on clicking on it? Cause that is a whole different issue.

Yes I created a sample page, many of them and even another one more time before responding to your post just to double check I wasn’t doing something silly.

Yes index.php is my home page and it calls the file that defines the constant and then loads the rest of the php via the include statement with the defined DIR <<< so half of it is working. the problem now seems to be calling the rest of the files if they are contained within a function

function myFunction(){
the rest of the function

By selecting a link a mean a valid link that the index.php produces in the menu. The link is what it should be I verified it, the template loads, but the includes and functions throw an error.

and they are using the same include (DIR.“/folder/file.php”); that loaded the first set of functions on the index.php

just thought I might add I am not a copy paste coder and I am not looking for some one else to write my code, I already wrote it and have a working version on my windows server. My linux server is what I am having a problem with. even though I see what directory is being searched for the file by the error thrown and I know the slashes are currently correct I plan on trying Anthony’s suggestion in the morning. DIRECTORY_SEPARATOR

Is there any reason at all, the server would use the defined directory correct the first time but not if it is called in a second “layer”? i am really trying to stay away from putting all files in the same folder if I can

The error message you posted showed your folders under public_html. Is this really the case? All I can think of is a permission problem under linux. But if they are under public_html then the web user should be able to find them. I assume you checked their read permission. Can you run your test scripts from the command line?

Is it your own linux box or a shared host? /home/user/public_html seems a bit strange. I would have expected a user specific directory there.

Your original post had:
if(!defined(‘DIR’)){define('DIR, dirname(FILE));}
Which contains a typo. I’m sure it has been fixed in the code you are running.

All I can suggest is that you paste an exact copy of the relevant portions of index.php, folder2/file.php, folder3/file.php

I’m thinking you still have a simple typo somewhere or we are not understanding exactly what you are trying to do. Grasping for straws.

One more thing - linux file names are case sensitive, windows are not.

Yes there was a typo in the post, i hand type not copy and paste I am sorry. I would much rather have the folder above the public_html which is why I wrote the file script way, I moved it to public_html for troubleshooting and will move it back to where it belongs once I fix this error.

I was always told to not display errors and definitely don’t display them for the world to see, but here goes.

the link = Memory Upgrade at MemoryMega

Warning: include(/home/memory/public_html/XXX/DB/dbGetProductInfo.php) [function.include]: failed to open stream: No such file or directory in /home/memory/public_html/XXX/Function/update-products.php on line 1

The code on line is 1:


Fatal error: Call to undefined function dbgetproductinfo() in /home/memory/public_html/XXX/Function/update-products.php on line 251

which really confuses me because the code on line 251 is:


Note the uppercase vs lower case name of the function??

btw the file loads many functions before it gets to line 251

Internally, php uses lower case for all functions and classes. So that is not a problem.

If you telnet/ssh into your server and do
ls -l /home/memory/public_html/XXX/DB/dbGetProductInfo.php
What exactly do you get? Be sure to copy/paste the file path exactly from your error message.

I tried your link, seems to work.

Always use error_reporting(E_ALL) when developing. Use a different strategy for production but if you have trouble then turn them back on.

I am on a shared hosting server I don’t think I have SSH access. Is there another way?

The link works yes, but select compare prices from one of the product listings

I imagine your host control panel has some sort of file browser? Just confirm DB is uppercase on the linux side and the rest of the file name is set as well.

It’s also possible your control panel offers some way to open a shell. Rather puzzling.

WOW, TY ahundiak,

I have no idea why but when I FTP’d my files from my pc to the server for some reason it kept changing the file name from




I couldn’t change the file name with FTP or file manager after I changed the file name it would revert back to dbgetProductInfo.php for some reason, the fix so far was to upload both files dbGetProductInfo and dbgetProductInfo but if I delete dbgetProductInfo dbGetProductInfo Reverts Back to dbgetProductInfo for some reason

I still haven’t figured out why(yet) but I am happy it’s working now I can get back to updating the overall script. Thanks again ahundiak

You can check out my current progress at Memory Upgrade at MemoryMega