SitePoint Sponsor

User Tag List

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 89
  1. #26
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $root can be used from anywhere in the app, include 'includes/' cannot.
    words, words...
    prove it with the code, please
    You would never set root to be where you are unless blah blah blah
    I am plain and simple guy.
    I just want to include /includes/file.php in my /news/archive.php
    and keep it if I move it to the /news/tech/archive.php
    Not to talk of such theoretical matters

  2. #27
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    prove what with code? that the app root dir might not be the same as the doc root? that the app could be installed in any sub dir and if you don't want to change a hard coded path then you can determine where you are?

  3. #28
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, another page hit! What an achievement!

  4. #29
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't even know what you're on about now.

    You can have $root = $_SERVER['DOCUMENT_ROOT']
    Or you can have $root = 'some/other/path/that/you/want/to/be/root/for/your/app'

    If you always use the first one, then you can't move your app.
    If you set your own root to what you want, then you can.

    How you do it is up to you. I'm sure you're aware that __FILE__ is the actual file not the working directory.

  5. #30
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am more on practical matters, not theory.

    In practice, we usually need some config file, being included into every our script.
    In this config file we can set up anything: paths to the modules, passwords to the database, etc etc. We can move certain app and then change only one line in config file - so, all your moving problems solved.
    But we need our config first.

    We have 4 ways to do it.
    1. hardcoded path. is not an option at all
    2. front controller. but usually we don't have one front controller for the whole site. More likely each app has it's own controller. So, the problem persists.
    3. $_SERVER['DOCUMENT_ROOT'], the monument of the absoluteness. I'd recommend it for the general use.
    4. auto_prepend_file. VERY useful, as it make manual inclusion unnecessary. But a bit complicated to use, basic .htaccess knowledge and some PHP configuration settings required. recommended for the experianced users, who can also choose a server by their own will

  6. #31
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that makes a little more sense from your side. But as usual you don't explain yourself. Sorry to say, it's not your english that is the problem, it's your attitude.

    You want to have a global init file in the doc root ... go for it access it via doc root and sleep well.

    If you want to allow someone to move your app, then you fail. I provided two ways to get around this. I would say that each app with it's own front controller would be a fantastic reason to determine the apps absolute path via a relative path. Surely any shared libs common to apps will be just above this layer?

    There are many ways to skin a cat. My point is simply that I prefer to use require ../config.php from an entry point and $root = dirname(__FILE__); in that config than alter the config file.

  7. #32
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, back to your point.

    The OP has 2 problems
    1. absolute addressing from the web root, for the CSS files, no matter from which catalog it being called
    2. absolute addressing from the filesystem root. I assume, with same purpose. To call /includes/file.php, no matter from which catalog it being called
    And your code fail to help him.

    That's plain and simple. We just want a way to use absolute path. But you give him the same old relative one!

  8. #33
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. was taken care of, webroot is /
    2. I gave a way to make an absolute path based on a relative one. ie ../something is a relative path, dirname(../something) is an absolute path (based on a relative path, but it will give you ABSOLUTE).
    2,a I gave an alternative way to do this if you were going to use arbitrary files like http://www.site.com/some/silly/page/...d/accessed.php

  9. #34
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gave a way to make an absolute path based on a relative one
    oxymoron (it's not a curse)
    you cannot get absolute from the relative, because of nature of these matters

  10. #35
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    er dirname will return the path, is that a fake path?
    getting late, pls explain what you mean a bit.

  11. #36
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah it's getting late

  12. #37
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    off to bed, but still curious: dirname returns the file sys name, so you can make an absolute file path from anywhere ... I don't see the problem, or need for doc root.

  13. #38
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with dirname you're finding absolute path for the parent script.
    but for the /includes/file.php you're still using relative. you presume that /includes/file.php is in the same directory. but it may not

    I am talking of absolute path for the /includes/file.php, with which it can be called from anywhere, like we do it for the CSS

  14. #39
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dirname(__FILE__)

    try it, it will tell you the path to the current file, no matter how or where it was included, or who the parent is.

    the goal is to set $root, whatever it is. then $root./whatever/the/hell is easy

  15. #40
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did it 1000 times.
    good trick to pick a file in the same directory, no matter what current directory is
    but it has nothing common with root thing and absolute path

  16. #41
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh no. correct myself.
    it will give you absolute path, yes. to your parent script.
    but it wont help you to include any other file, unless you know it's relative path from your parent script

    I thought I've found right explanation in post before
    I am talking of absolute path for the script that goes to be included

    or may be you're wrong with dirname/__FILE__
    dirname(/whatever/the/hell/file.php) would return /whatever/the/hell
    dirname(/whatever/the/file.php) - /whatever/the
    dirname(/whatever/file.php) - /whatever

    so, it's different for the each directory. You cannot call a root such a varying thing.

  17. #42
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no not parent script, actual file. You can include config.php from /whatever/somewhere/script.php and __FILE__ will report it's actual path. Hence you have an abso path and can define a $root ... only thing is if you need to look for it, or just require ../config.php

  18. #43
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, I hugely appreciate the discussion you guys had above because it shows me all the ways that are viable solutions; in addition, it reveals that this is not a simple topic to deal with.

    Secondly, I think for my code, I'm going to try hash's way of having a root.txt in whatever root folder you choose, building a while loop to find that root.txt, then setting that directory as your root, and building all other absolute paths around that root folder. In this method, you choose your own root, rather than dealing with what you are given by the server.

    I have not tried this method yet, but I will soon. The only manual work I could see involved in it is that you would have to put a root.txt into your new host folder (if the host is changed), but that's not much work at all. PHP-includes, I believe (though have not tested yet), will also work because again, you will be using absolute path names from wherever you specify.

    Again, thanks a lot for the discussion and if I have any other problems, I'll shoot them back to the experts.

    PS. Has anyone found a solution for the CSS absolute path names, since I can't use $root in .css files? Perhaps I have to be more manual about this and change each absolute path name depending on the host (root folder)?

  19. #44
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote this solution 2 times already in this topic

  20. #45
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    I wrote this solution 2 times already in this topic
    Ah my apologies:
    1. absolute addressing from the web root, for the CSS files, no matter from which catalog it being called
    Yes, I see your point, and I did that with my CSS files. However, again we run into the same problem.

    If your web root changes (you switch from localhost to virtual host), must you change ALL the absolute image/link/etc. paths?

  21. #46
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you try it first, if you can't realize that you have the same web root on the every server in the world?

    what's going to be changed?
    image/link/etc become fun/pics/free or what?

  22. #47
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    why don't you try it first, if you can't realize that you have the same web root on the every server in the world?

    what's going to be changed?
    image/link/etc become fun/pics/free or what?
    Basically, if I work on my localhost, the absolute path name is something like /wamp/www/~sitenameroot

    If I upload this website to a virtual host, the absolute path name becomes:
    /home1/~something/~username/public_html

    This means that everytime I do local testing on localhost and see the changes, I must change all of the absolute paths to visibly see the changes on the virtual host.

    Hopefully I'm being clear enough in my explanation.

    The easiest solution, of course, would be to use relative path names so that all my files are interlinked despite the changing absolute path. However, I soon as I dabble in PHP-includes, this system falls apart.

    The while-loop to find the $root seemed good, but under basic testing (certainly not extensive, so feel free to prove me wrong), it goes too deep into my harddrive C:/
    Code PHP:
    $root = dirname(__FILE__);
    while(!file_exists($root.'/root.txt')) {
        $root = realpath($root.'/..')
    }
    Upon viewing the source of my file:
    Code HTML4Strict:
    <link rel="stylesheet" type="text/css" href="C:/wamp/www/website_name/stylesheets/home.css" />

    Therefore, home.css is not loading (even though the path name is absolute) because localhost does not recognize its depth (C:/). Perhaps it is because I used the function realpath()?

    --

    My main problem is that: if I'm building a website for myself, I can map my own localhost directories to be exactly the same as the one my virtual host uses (eg. create the folders (username)/public_html into my wamp/www folder; it is inefficient, but works, I assume). However, as soon as I give this to a client who throws this up on his own virtual host, the absolute path names are immediately invalid. What I'm looking for is more code flexibility while still being able to use PHP-includes at different sublevels of my directory.

  23. #48
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /stylesheets/home.css

  24. #49
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You must distinguish your filesystem path from webserver path
    /home1/username/public_html/template/ is the filesystem path
    but this path doesn't exist in the virtual web-server.
    your browser don't ever know of it's existence!
    the web-server path begins after public_html.
    so, for the browser it will be /template/
    so, you don't need any single variable to make your virtual path absolute. it is always the same
    Look at the URL of this very page:
    http://www.sitepoint.com/forums/show...76#post4530676
    It has protocol part, hostname part and request part, which consists of path, filename and query string.
    notice the path?
    /forums/showthread.php - <--absolute path. would remain the same if you call it from anywhere. that's the meaning of the word "absolute"
    how on the earth we have PHP developers even with customers, who can't write even basic HTML?

  25. #50
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    /stylesheets/home.css
    Considering this is in "header.php", this will not work because "header.php" is included in different files of different subdirectory levels.

    Therefore, for sublevel 1, it may be able to find the stylesheets folder, but as soon as "header.php" is PHP-included into sublevel 2, it will not be able to find the directory.

    If I'm not understanding something or being unclear with the exact problem I'm having, please say so.


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
  •