Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include_once, relative path in php

I have 3 files: home, failed_attempt, login.

The file home and failed_attempt all refer to login file.

The annoying thing is that they throw a mistake saying that the login file doesnt exist. home will throw an exception if i do this, but failed_attempt wont.

  include_once("../StoredProcedure/connect.php");

include_once("../untitled/sanitize_string.php");

and if I do this:

   include_once("StoredProcedure/connect.php");
   include_once("untitled/sanitize_string.php");

the opposite happens, failed_attempt throws an exception , but home, wont. How do I fix this..

Do I tell the include to go up a page by putting this ../ , and therefore home.php doesnt need to go one page up therefore it throws that exception..

How can I make it so both files accept those inclueds as valid.. perhaps not relative to where they are placed.. i.e. without that ../

Directory structure:

PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files
like image 926
Matrix001 Avatar asked Oct 20 '11 12:10

Matrix001


1 Answers

For the beginners this will help to understand. Use simply the following based on your relative path:

//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');

OR

//include file from parent's child directory:
include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');

OR

//include file from own child directory:
include_once('StoredProcedure/connect.php');

OR

//include sibling files from same directory:
include_once('connect.php');
like image 163
Reza Mamun Avatar answered Oct 27 '22 01:10

Reza Mamun