Hapless Golok
Hapless Golok

Reputation: 1

How to use a file from public_html folder to any folder?

I want to use dbconnect.php file from public_html folder to public_html/profile/. I am trying to do this with this code in public_html/profile/index.php file:

<?php include'/dbconnect.php'; ?>

but it give an error.

Upvotes: 0

Views: 64

Answers (2)

Emylee
Emylee

Reputation: 191

<?php include'../dbconnect.php'; ?>

Though, the best way to write it is:

<?php require_once("../dbconnect.php"); ?>

If dbconnect.php is in your public_html folder and the file in which you are using the include is in the public_html/profile/ folder, you need to use ../ to get to the parent folder.

Putting it like you have would have Linux looking for dbconnect.php in the root of the operating system; which obviously, it's not there!

So whenever you want to go back a folder, or 2, you can do stuff like ../../file.php or even go back, then enter another folder ../../public_html/file.php.

require_once will insure no more than one call to that file is ran in an instance, so it's helpful to avoid mistakes!

Upvotes: 3

Andreas
Andreas

Reputation: 23958

<?php include'../dbconnect.php'; ?>

Or you can also use:

<?php include $_SERVER['DOCUMENT_ROOT']."/dbconnect.php'; ?>

Upvotes: 1

Related Questions