SitePoint Sponsor

User Tag List

Page 1 of 4 1234 LastLast
Results 1 to 25 of 89
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Direct Path Names for Hosting Using PHP

    I'm having a fairly simple problem which I think can be solved, but I'd like to know if there is a more efficient solution.

    When building a website on my personal computer, I use WAMP and the direct path name involves something with "localhost".

    However, when I upload those same files to my virtual host, the direct path names that I had set for images, links, etc. no longer work because the direct path name is not the same. In short, the direct path name depends on the host, in which they are different between my personal computer and the virtual host.

    My solution is to assign the full direct path name to a PHP string, and then set that PHP string to everything that needs a direct path name. I will also have to change the direct path name every time between testing on my personal computer and uploading the files to the Internet.

    While it's certainly doable, is there a more efficient solution to this problem? I'd be greatly appreciative. Thanks!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd say absolute path is obligatory.
    But it is very important not to mess URL and path.

    So, the best way to handle site paths is to use not URL but path only, absolute one.
    "/images/pic.jpg" would be all right on any server.

    On the other hand, there is a problem with filesystem paths which, in it's turn, shouldn't be confused with site paths.

  4. #4
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Surely this exactly what the <base href=""> element is for?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Another solution is to run a DNS server on your own PC, like Posadis so you can have top level domains on your own pc.

    For example you can create a dev.mywebsite.com on your own PC and www.mywebsite.com on the hosted domain. The path's don't change then.

    Of course you also need apache virtual hosting on your own PC when you this.

    I've got this set up on my own PC, and it works like a charm

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Another solution is to run a DNS server on your own PC, like Posadis so you can have top level domains on your own pc.

    For example you can create a dev.mywebsite.com on your own PC and www.mywebsite.com on the hosted domain. The path's don't change then.

    Of course you also need apache virtual hosting on your own PC when you this.

    I've got this set up on my own PC, and it works like a charm
    ...or you can just edit the hosts file and achieve exactly the same thing

    c:\Windows\System32\drivers\etc\hosts - edit with notepad (you need to run notepad as Administrator if on W7 or Vista) and add a line for sub.domain.com or whatever, and the ip address it points to.

    All my dev projects happen in subfolders anyway, so my projects are coded to account for this, and allow it to be placed in a subfolder, rather than assuming it will always be run from a top level domain.

  7. #7
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another solution is to run a DNS server
    is not a solution but nonsense

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps there is no simple way to do this?

    Relative path names would not work because if I'm using PHP-includes, the same header.php would be included in different pages (of different subdirectories).

    I'm hoping for a solution regarding an absolute file path.

    For example, my host's directory is similar to:
    /home1/username/public_html/website/(then my folder)

    My local directory is simply:
    /wamp/www/(my website folder)

    My solution as of now is to simply assign "home1/username/public_html/website/" to a PHP string like $direct_path_name, and then change it to $direct_path_name = "/wamp/www/" when working on the localhost.

    I believe this way will work, it just seems repetitive and inefficient.

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For html /images/whatever/stuff.jpg is always going to work.
    For php you need either a root to start paths with, or relative paths from the current working directory to where you want to go. If it's only for includes then you can just set the include path and be done.

    To set the path you can either know where you are (eg front controller is always in web root) and build from there, or you can put a marker file in the root and look for it to determine where you are.
    PHP Code:
    $root dirname(__FILE__);
    // /home/~user~/www on one server
    // /var/public_html on another
    include $root.'/includes/file.php'//always works 

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hash, that was precisely what I was looking for. Thank you very much!

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    is not a solution but nonsense
    Care to elaborate why you think this is nonsense?
    I've got it set up here with a wildcard TLD and mass virtual hosting. I can just create a folder "myproject" en my DNS server and Apache's mass virtual hosting automatically allow me to acess that project through http://myproject/
    So I have a top level domain and I can use absolute paths for images etc, as I would on http://www.myproject.com/

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Care to elaborate why you think this is nonsense?
    I've got it set up here with a wildcard TLD and mass virtual hosting. I can just create a folder "myproject" en my DNS server and Apache's mass virtual hosting automatically allow me to acess that project through http://myproject/
    So I have a top level domain and I can use absolute paths for images etc, as I would on http://www.myproject.com/
    Why not just code it so it can work anywhere, rather than making the assumption it will always be on a top level domain?

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Going to have to agree with Stormrider on this, if the customer can change their mind, they will.

    You'll deploy their application to i-love-bacon.com and before you know it, they've changed their domain name to i-love-ham.com and your app blows up.

    Code for change, it's the only thing you can depend on.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,077
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I make the assumption the websites I make will run on a TLD bacause out of the 50 or so projects I did so far only 2 of them didn't (so about 4%).
    It's a good thing to take into account that things change, but if you want to take into account every "what if" you're making things more complicated than they could be.
    In the cases the website is not run on a TLD I know this beforehand and then of course I take this into account.
    Anthony, which domain the website runs on is no issue, I could put it on any TLD I like without any changes to code.

  15. #15
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by alexp91k View Post
    hash, that was precisely what I was looking for. Thank you very much!
    I have a similar problem now:

    All of my CSS files (because they are included in different sublevel pages) use absolute path names. Since I can't use $root and PHP in these CSS files, and using relative path names is impossible, is there any solution for this?

    ---

    Do all websites that have 2 or 3 sublevel directories have this problem (when testing between the local machine and host)? I feel like I am doing something very wrong.


    Edit::
    This is a small portion of header.php, which is included into many different files on different sublevels of directories.
    Code PHP:
    <?php $root = dirname(__FILE__); ?>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!--Meta Tags Go Here -->
    <!--Meta Tags Go Here -->
    <title>Title</title>
     
    <link rel="stylesheet" type="text/css" href="<?php echo $root ?>/stylesheets/home.css" />
     
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript" src="<?php echo $root ?>/javascripts/animation.js"></script>
    <script type="text/javascript" src="<?php echo $root ?>/javascripts/jquery.ie6.js"></script>
     
     
    <!--[if IE 7]>
    <link rel="stylesheet" type="text/css" href="<?php echo $root ?>/styles/ie7.css" />
    <![endif]-->
     
    </head>

    My problem here lies in that if this page is included on every page of different subdirectory levels, the $root will change depending on where that __FILE__ is located. Therefore, the code will break on certain subdirectories.

    At least, I think so. I'd really appreciate any strategies to overcome the CSS difficulty and the PHP absolute path including. Thank you!

  16. #16
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Care to elaborate
    ScallioXTX, it solves the problem that doesn't exist.

    Alex, blame my bad English, I can't do explanations properly.
    Although I answered you already

    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"

    So, if your styles dir lays in the root of the webserver,
    /styles/ie7.css is enough.
    <?php echo $root ?>/javascripts/jquery.ie6.js is wrong, because browser doesn't have access to your hard disk!

    And well, hash, I can't agree with you too
    your method is sort of relative path,
    it solves the problem with 2 hosts but won't work if we move 1 folder deeper.
    It works only if front controller used.
    if not - we have to use something else
    $_SERVER['DOCUMENT_ROOT'] is my favorite

  17. #17
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And well, hash, I can't agree with you too
    your method is sort of relative path,
    it solves the problem with 2 hosts but won't work if we move 1 folder deeper.
    It works only if front controller used.
    if not - we have to use something else
    $_SERVER['DOCUMENT_ROOT'] is my favorite
    That is no good if you don't know where your app has been installed.
    There are several ways to do things, I mentioned an alternative that doesn't require knowing where you are, and also doesn't require knowing where you are in relation to true root - your apps root doesn't have to be the servers.

  18. #18
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I mentioned an alternative that doesn't require knowing where you are
    No, it does.
    That's what I am talking about.
    $root = dirname(__FILE__); won't give you real root
    but just relative path to your script.
    run your code in the /fourums/ dir - and it will fail.

    While $_SERVER['DOCUMENT_ROOT'] is the real root, absolute one, if properly set (usually does)

  19. #19
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think we're talking about diff things. There is server root, and also an app root that only your app cares about. It could be anywhere ... /var/www/some/directory/project/apps/forum/ (or it could be identical to server root)

    So for your app you either know where you are and go up x number of directories to get to forum/ or you don't, and mark forum so you will know when you found it. Then your apps root is set, include $root.'includes/file.php' is /var/www/some/directory/project/apps/forum/includes/file.php
    Using $_SERVER would fail horribly.

  20. #20
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but my app can utilize some folder structure
    and once I set it's "root" in the /var/www/some/directory/project/apps/forum/
    it won't help me in the /var/www/some/directory/project/apps/forum/polls/
    if I don't use front controller.

    your method utilizes relative path, not absolute. that's it
    so, you don't really need your code
    include includes/file.php'; would have the same effect

  21. #21
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like I said, two methods:
    1. you know where you are (eg with front controller)
    * set root based on where you are
    * $root = realpath(dirname(__FILE__).'../'
    2. You don't know where you are
    * go up till you find root.txt
    * set this as root

    End of the day you have your apps root as the full path to the directory you want as your apps root, so no matter where you include $root.includes/file.php it works, instead of only working from /forum

  22. #22
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    End of the day you have your apps root as the full path to the directory you want as your apps root, so no matter where you include $root.includes/file.php it works, instead of only working from /forum
    Your code won't help with it.
    it works from the forum only.
    if you call it from the forum/index.php it will point to the 'forum/includes/file.php'
    and if you call if from the /forum/polls/index.php it will point to the 'forum/polls/includes/file.php' and fail.

    your method is relative, not absolute
    *sigh*

  23. #23
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does setting the $root to be /var/www/forums/ and using include $root.'includes/file.php' EVER include anything other that /var/www/forums/includes/file.php no matter where it is used?

    *sigh*

    PHP Code:
    $root dirname(__FILE__);
    while(!
    file_exists($root.'/root.txt')) {
        
    $root realpath($root.'/..')

    If you know the app root is always one up from the front controller, then just skip the while. Either way, what you end up with is the full absolute path to the root you want as your apps root (which may or may not be the same as the doc root).

    The problem:
    Need a root folder to reference other folders by (cache, log, include, lib whatever)

    Solution 1:
    Use $_SERVER['DOCUMENT_ROOT']
    This won't always work. If your app gets moved one folder down, then all is broken.

    Solution 2:
    You know where the root is in relation to the boot script/front controller.
    Set $root accordingly, as an absolute path. Now you don't care where you are moved.

    Solution 3:
    You don't know where you are in relation to the app root. Put a marker in the root, and move upwards looking for it, set $root to this absolute path.

    Solution 4:
    Use a config directive and define a root. As with #1, has to be updated if your app moves directories.

    Solutions 5-79????

  24. #24
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahaha that's funny code.
    never thought of that. this one is near to usable one
    but I'd hate to write such a construct to get my config.
    I would prefer $_SERVER['DOCUMENT_ROOT']./../config/ini.php

    As of your previous approach, it's
    1. useless
    2. wrong.

    Case one:
    Assume we call it from the /users.php
    and our includes folder's path is /includes/
    So, we can replace your code
    PHP Code:
    $root dirname(__FILE__);
    include 
    $root.'/includes/file.php'//always works 
    with just
    PHP Code:
    'includes/file.php'
    with same result

    verdict: useless

    Case two:
    Assume we call it from the /news/archive.php
    and our includes folder's path is /includes/
    in this archive.php we have your code
    PHP Code:
    $root dirname(__FILE__);
    include 
    $root.'/includes/file.php'//always works 
    and "File not found" error
    because your $root variable named wrong. it has nothing to do with root as it merely relative path to the calling file and nothing more

    verdict: wrong

  25. #25
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    ahaha that's funny code.
    never thought of that. this one is near to usable one
    but I'd hate to write such a construct to get my config.
    I would prefer $_SERVER['DOCUMENT_ROOT']./../config/ini.php
    And if your config is in $_SERVER['DOCUMENT_ROOT']/path/to/moved/app
    Then what do you do? Hard code it? Nice one.

    Quote Originally Posted by Shrapnel_N5 View Post
    As of your previous approach, it's
    1. useless
    2. wrong.

    Case one:
    Assume we call it from the /users.php
    and our includes folder's path is /includes/
    So, we can replace your code
    PHP Code:
    $root dirname(__FILE__);
    include 
    $root.'/includes/file.php'//always works 
    with just
    PHP Code:
    'includes/file.php'
    with same result
    this is nowhere near the same result.
    $root can be used from anywhere in the app, include 'includes/' cannot.

    Quote Originally Posted by Shrapnel_N5 View Post

    Case two:
    Assume we call it from the /news/archive.php
    and our includes folder's path is /includes/
    in this archive.php we have your code
    PHP Code:
    $root dirname(__FILE__);
    include 
    $root.'/includes/file.php'//always works 
    and "File not found" error
    because your $root variable named wrong. it has nothing to do with root as it merely relative path to the calling file and nothing more

    verdict: wrong
    Again you fail a bit to grasp what is happening.
    You would never set root to be where you are unless you know exactly where you are. and you would never use inculdes/ to include system/

    try to think this one through there shraps. particularly before all the lol fail haha wrong you like to throw around.


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
  •