SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Relative vs. Absolute Paths

    Which is the best way?
    Using relative paths or using absolute path to include/require scripts and to work with the whole PHP project...

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    it depends on what you are doing with them!
    For generic things like database connections you can happily use absolute paths but image uploading require relative paths.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I tend to use relative paths, but that's because I don't buy clients' hosting before I upload, I just make all of the files inside a folder on my server.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    but image uploading require relative paths.
    it does? I've always used absolute paths for something like an image upload function... I think it just comes down to personal preference... in any case if you move something your still gunna have to update the path to it...
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    lol, sometimes they do!
    I spent 3 hours rewriting an upload function because it wouldn't work - it was because I had an absolute path to the upload directory.

    But yes, generally it's personal preference.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I have class or function pages that must find each other and should always be in the same place relative to each other, I use something like...

    PHP Code:
    require_once(dirname(__FILE__).'/functions.php'); 
    There shouldn't be anything wrong with using a full path to an upload folder, it should always be correct. Better that then relying on the CWD of the script that is executing to affect the path you have to use.

  7. #7
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    And what about this? I put this the entry point of my application (index.php)
    And search for directories, files based on this 'root' path...

    PHP Code:
    $rootPath realpath(dirname(__FILE__)) .'/';
    $rootPath str_replace('\\','/',$rootPath); 
    The str_replace is for changing the slash (\) to forward slash (/) because my developer server is on windows machine and the client's production server is on linux.

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use relative paths; There is an issue in regards to performance but it's neglible and isn't even worth taking into account in any case anyways. If you have PHP5 then use __autoload( ... ); otherwise you are more or less limited to setting the include path only.

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Relative paths are way more efficient than absolute paths because you save on the lookup of the domain.

    Relative paths also means that the copy on your computer works the same as the copy on the site because the relative paths are the same but the absolute paths are different.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •