Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Help with relative paths in includes.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with relative paths in includes.

    Hello everyone,

    I hope you've seen something like this in popular (PHP)frameworks or wikis or bulletin boards(like dokuwiki). I hope you've realised that these softwares let you change the names of their directory and the software still works(without any other efforts to modify the main script). They use something like a "dynamic" include that finds the needed file where ever it is. I was making a script where I needed a similar thing but unfortunately even with hours of googling, I couldn't find it. So I was wondering if anyone has any idea to achieve this?

    Any help would be greatly appreciated. Thanks for your time and patience.

  2. #2
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    errr...

    PHP Code:
    <?php include("path/to/file.php"); ?>
    I understand you can also use require(); the same way.

    This must NOT be what you mean... is it?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    To tell you the truth, it is but not completely(about 33&#37.

    Let me show you an example:

    The problem with relative URLs in include is:

    PHP Code:
    <?php
    include("/path/to/file.php");
    /*... bla bla bla ...*/
    ?>
    now, the until the "file.php" is inside the "to" directory inside the "path" directory, the script will work but what if I wanted to change the name of the "path" directory to, say "relpath"(where rel means relative), now I need to modify the path in all the scripts. What I want is a dynamic way where I don't need to modify the code when I change the directory name(because when the size of the code is huge, it is almost impossible for any "normal" user to go back to the main source code and change each and every path).

    I hope I was able to clear my point.

    Thanks for your help though.

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    PHP Code:
    $path "path/to/";
    include 
    $path."filenamehere.php"
    And keep reusing the variable in every instance in your code. So then if you have to change the folder location/name simply change the variable "path"
    - Mike

  5. #5
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't know how to do this, but I believe that this tutorial is what you are looking for: http://www.pixel2life.com/publish/tu...put_buffering/ . Hope thats what you need
    Thanks DD, you saved me countless times

  6. #6
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Titan85: Been there, done that. Thanks anyways.

  7. #7
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    So Some Sort of Code that Searches the Directory for a folder that Contains a Folder which Contains A Specific File?

    This Could be Accomplished Using A Multi Dimensional array Generated like this...

    $dir = array (
    "folderA" => array (index.php, include.php, pic2.bmp),
    "folderX" => array (zero.bmp, header.jpeg, .htacsess),
    index.php,
    picture.jpeg
    )
    then search it for a certain folder/file
    So $dir[folderA][1] would be returned as folderA/include.php

    I have No Idea How This Could be accomplished, but i Do Know its possible.
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    mburt is correct: this is the method I've seen in every major PHP application. The path is stored as a variable in a separate file as close to the root of the application as possible ("config.php" for example), then included into other files, from whence it is used as part of the include path for further files.
    Code:
    include("/path/to/file.php");
    That's an absolute path.

    You should also look at the tool sed. It's capable of doing recursive search/replace operations.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Quick interjection...

    I have been working on an SQL/PHP problem for a while, and this last post has given me a new idea...

    can I ask what the "corrrect" syntax is for a single array?

    Here is what I am looking at, please correct or confirm for me:

    PHP Code:
    $name = array (name1name2name3name4);

    //later on in the source...

    value = ?first_name $name

    What I am trying to get around is the *:* means either/or, so by making the other option an array, I could have more choices added to the second half, yes?
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    You didn't supply an initial argument?
    Code:
    value = ?first_name : $name;
    Shouldn't it be:
    Code:
    value = argument?first_name : $name;
    I'm confused on this issue...
    - Mike

Bookmarks

Posting Permissions

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