What is the difference, or advantages between http://site/info/phpFile.php and $_SERVER['DOCUMENT_ROOT'] ."/info/phpFile.php?

I've also read a few comments on the internet that seem to say one should avoid relative paths (../info/anotherfile.php). Any thoughts?

Thanks,
Robkir

    robkir;11041129 wrote:

    What is the difference, or advantages between http://site/info/phpFile.php and $_SERVER['DOCUMENT_ROOT'] ."/info/phpFile.php?

    One is a URL (which has nothing to do with file paths), and one is a file path (which has nothing to do with URLs).

    As for the advantages... well, same thing, really; if the value you need is supposed to be a file path, the latter has the advantage of being the only applicable choice. Similar advantage for the former if what you require is a URL instead.

    robkir;11041129 wrote:

    I've also read a few comments on the internet that seem to say one should avoid relative paths (../info/anotherfile.php). Any thoughts?

    My first thought is... if there's absolutely no good use case for them, then why do they exist.

    I think it really depends on what your application is doing and/or what kind of assumptions you make about how your codebase will get used.

      There is an important difference between URLs and file system paths in contexts such as this:

      require_once "http://localhost/info/phpFile.php";
      require_once $_SERVER['DOCUMENT_ROOT']."/info/phpFile.php";
      

      While both lines are ultimately referencing the same file, there is a key difference in what happens. In the first case (via URL), what will be included is the output of that PHP file after being processed by the web server and PHP compiler. In the second case (via the file system), what will be included is the actual source code of that file -- which 99.999%* of the time is what you want to do.


      *64.9% of all statistics are made up.

        NogDog;11041133 wrote:

        While both lines are ultimately referencing the same file

        I'd switch "ultimately" out for "maybe"... you never know what actual file (if any) a request is mapped to just by looking at the URL alone. Assuming no trickery is going on for the sake of the given example, however, yes, the rest is what I was hinting at in a roundabout fashion. 😛

          NogDog;11041133 wrote:

          There is an important difference between URLs and file system paths in contexts such as this:
          In the second case (via the file system), what will be included is the actual source code of that file -- which 99.999%* of the time is what you want to do.

          Thanks guys. I appreciate the in-depth explanations.

          robkir

            Write a Reply...