zafrada
zafrada

Reputation: 61

How to correctly reference hosting root?

Say I have a "test.db" file under www.myhosting.com/data/test.db. I need to reference this file from www.myhosting.com/inc/functions.php

What would be the proper way to reference the file?

$filename = '../data/test.db';

is not appropiate because www.myhosting.com/index.php would try to go to a non existant parent dir ../data

Upvotes: 1

Views: 503

Answers (2)

r92
r92

Reputation: 2813

First way:

$filename = $_SERVER['DOCUMENT_ROOT'] . '/data/test.db';

Second:

$filename = getcwd() . '../data/test.db';

Upvotes: 1

zerkms
zerkms

Reputation: 254916

Create (if you still doesn't have one) config.php file where you define absolute path to your root and include it in every your script. After that - use that constant to assemble path.

Ie:

$filename = ROOT_PATH . '/data/test.db';

Upvotes: 1

Related Questions