Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts

    The path of my application?

    Web application programming still plagues me, basic things such as this... What is the best method to reference the path of where my application is installed?

    The path could vary...
    root/my_app/
    root/some_dir/my_app/
    etc...

    Also the name of the "my_app" directory could vary...
    root/this_app/
    root/some_dir/this_app/
    etc...

    My application has a few sub directories with files that need to include a common file, but I'm not sure how to go about writing the include statement so it will work regardless of the directory structure. I have looked at the manual, but if the answer is there it was lost on me. Any help is appreciated.

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    I'm not really sure what you mean exactly... I guess depending on how complex you want to get there are lots of different ways. The simplest method I use is to create a variable that contains the path back to the base of all your files. From there I have predefined folders to store my programming in.

    PHP Code:
    // How ever many folders you need to go back on a particular script.  One folder back.
    $base '../';  // For two folders back '../../' and so on.

    // Define my programming folders.
    define ('SCRIPTS''Scripts/');
    define ('PRESENTATION''Presentation/');

    // Which basically finds the file one folder back in the scripts folder with the name.
    include $path SCRIPTS 'my_database_connection.php';

    // Some Code

    // Which basically finds the file one folder back in the presentation folder with the name.
    include $path PRESENTATION 'template_for_page.php'
    So if this page was on your server at yoursite.com/folder_1/this_script.php

    You would place the $base variable equal to '../' if your includes where in the root and '../../' if they were behind the root.

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Coyote, I think you got my meaning. Your example is fine when the actual directory structure is known. The issue is that some user might install it on the root and another user might install it 2 or 3 directories deep. If I have a way to find the base directory of my application, it's easy sailing... just not sure how to do that.

    I'm thinking this must be a common issue for those making web apps (whatever you want to call it) that they distribute, no? I have never done this before. In the world of VB/VC it's like calling app.path... returns the path of the application regardless of where it's installed.
    Last edited by mothra; 09-12-2009 at 02:21 AM.

  • #4
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    PHP Code:
    define('PATH'dirname(dirname(__FILE__))); 
    should work
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Zangeel, I wrote a lengthy reply, then erased it in lieu of the following simple question.

    If I execute a script from my application's "install directory" to get the dirname of that location how do my other scripts get access to that data? Even if I define it as a constant I must then include that script into all other scripts that need access to the constant, correct? The only other option I know of is to put it into a session variable... but this seems like a hack.

  • #6
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Let's say you're three folders up from your public_html i.e. public_html/folder1/folder2/folder3(you're here)

    PHP Code:
    define('PATH'dirname(dirname(__FILE__)));

    echo 
    PATH '/'
    Will show

    Code:
    /home/{username}/public_html/{folder1}/{folder2}/<<directory you're in (doesn't show)>>
    Put it a folder up again it shows

    Code:
    /home/{username}/public_html/{folder1}/{folder2}/{folder3}/<<you're here so it doesn't show this folder name>>
    Straight forward, so you can define this constant in any file, such as install files that you need a to find a path in.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Posts
    262
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks again sir, but I'm going to have to come back and post the long version... please bare with me

    Until I can get back and do that, I'm not after the path of the working directory, I'm after the path of the first directory no matter what my working directory is. I will come back to clarify, but I have to run right now...

  • #8
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    Haha, OK, well you can always just explode the file like

    PHP Code:
    $e explode('\\'dirname(dirname(__FILE__)));

    var_dump($e); 
    (Testing n my localhost, so I'm using \ instead of /) but this gives this output:

    Code:
    array
      0 => string 'C:' (length=2)
      1 => string 'Users' (length=5)
      2 => string 'ComputerName' (length=6)
      3 => string 'AppData' (length=7)
      4 => string 'Local' (length=5)
    So you can put the differnt parts of the path in the url. So if you wanted to find a file from public_html you'd start with something like

    PHP Code:
    $path explode('/'dirname(dirname(__FILE__)));

    print 
    $path[0]; // home
    print $path[1]; // username
    print $path[2]; //public html 
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •