Include Connection file

Discussion in 'PHP' started by cutebird75, Apr 23, 2008.

  1. #1
    Hello every body!
    I am not very genious in programming. I am working on a online system.
    I have my connection file in "home/config/connection.php" directory.
    Now I am accessing this file through include ('../connection.php') on first level and include('../../connection.php') on second level. I have to mention all this in each file.
    but the problem is that if I have to change my directory structure or relocate files to a new place then I will have to change path manually in all files. if there is any specific solution to solve this problem.please tell me about it in datail.
    Thanks alot.
     
    cutebird75, Apr 23, 2008 IP
  2. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    you can create a variable
    $directory = '../../'

    and use

    include($directory.'connection.php');

    so when you change the directory structure, you will asign a new value to $directory and ...done
     
    nebhotep, Apr 23, 2008 IP
  3. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If I have to include (for example) a file called 'include_this.php' which is located two directories down and then up into a directory called 'config', I use:
    include( dirname( __FILE__ ) . '/../../config/include_this.php' );

    This also helps for when there are problems with including files in different directories that then want to include files in other, different directories and so on...
     
    TwistMyArm, Apr 23, 2008 IP
  4. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    nebhotep is a genious!!!
     
    Sabbir, Apr 23, 2008 IP