Have script path with unresolved symlinks in PHP?












0















Let's say we have a web directory with the following paths:



framework/
index.php
...
instance/
index.php -> ../framework/index.php (symlink)
...


If we now make a request for (...)/instance/index.php PHP will resolve the symlinks and set __FILE__ as (...)/framework/index.php



Is there a way around this so e.g. dirname(__FILE__) would be (...)/instance?



EDIT: Since it has been pointed out, I'm actually not sure if PHP itself is resolving the links or something else. In my case I'm using nginx as a webserver with php-fpm as FastCGI.



(The background is that I want to have multiple instances of a CMS or Framework referencing a common code base.)










share|improve this question

























  • Are you sure PHP is doing this and not the webserver?

    – Barmar
    May 25 '16 at 19:56











  • No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

    – Arsylum
    May 25 '16 at 20:03






  • 1





    This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

    – Willker Moraes Silva
    Feb 16 '17 at 2:07
















0















Let's say we have a web directory with the following paths:



framework/
index.php
...
instance/
index.php -> ../framework/index.php (symlink)
...


If we now make a request for (...)/instance/index.php PHP will resolve the symlinks and set __FILE__ as (...)/framework/index.php



Is there a way around this so e.g. dirname(__FILE__) would be (...)/instance?



EDIT: Since it has been pointed out, I'm actually not sure if PHP itself is resolving the links or something else. In my case I'm using nginx as a webserver with php-fpm as FastCGI.



(The background is that I want to have multiple instances of a CMS or Framework referencing a common code base.)










share|improve this question

























  • Are you sure PHP is doing this and not the webserver?

    – Barmar
    May 25 '16 at 19:56











  • No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

    – Arsylum
    May 25 '16 at 20:03






  • 1





    This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

    – Willker Moraes Silva
    Feb 16 '17 at 2:07














0












0








0








Let's say we have a web directory with the following paths:



framework/
index.php
...
instance/
index.php -> ../framework/index.php (symlink)
...


If we now make a request for (...)/instance/index.php PHP will resolve the symlinks and set __FILE__ as (...)/framework/index.php



Is there a way around this so e.g. dirname(__FILE__) would be (...)/instance?



EDIT: Since it has been pointed out, I'm actually not sure if PHP itself is resolving the links or something else. In my case I'm using nginx as a webserver with php-fpm as FastCGI.



(The background is that I want to have multiple instances of a CMS or Framework referencing a common code base.)










share|improve this question
















Let's say we have a web directory with the following paths:



framework/
index.php
...
instance/
index.php -> ../framework/index.php (symlink)
...


If we now make a request for (...)/instance/index.php PHP will resolve the symlinks and set __FILE__ as (...)/framework/index.php



Is there a way around this so e.g. dirname(__FILE__) would be (...)/instance?



EDIT: Since it has been pointed out, I'm actually not sure if PHP itself is resolving the links or something else. In my case I'm using nginx as a webserver with php-fpm as FastCGI.



(The background is that I want to have multiple instances of a CMS or Framework referencing a common code base.)







php linux path symlink






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 6:59









Sz.

1,3111824




1,3111824










asked May 25 '16 at 19:43









ArsylumArsylum

317112




317112













  • Are you sure PHP is doing this and not the webserver?

    – Barmar
    May 25 '16 at 19:56











  • No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

    – Arsylum
    May 25 '16 at 20:03






  • 1





    This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

    – Willker Moraes Silva
    Feb 16 '17 at 2:07



















  • Are you sure PHP is doing this and not the webserver?

    – Barmar
    May 25 '16 at 19:56











  • No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

    – Arsylum
    May 25 '16 at 20:03






  • 1





    This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

    – Willker Moraes Silva
    Feb 16 '17 at 2:07

















Are you sure PHP is doing this and not the webserver?

– Barmar
May 25 '16 at 19:56





Are you sure PHP is doing this and not the webserver?

– Barmar
May 25 '16 at 19:56













No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

– Arsylum
May 25 '16 at 20:03





No. I just guessed from the php manual: __FILE__ The full path and filename of the file with symlinks resolved. Could be this is not done by php itself though.

– Arsylum
May 25 '16 at 20:03




1




1





This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

– Willker Moraes Silva
Feb 16 '17 at 2:07





This solve my problem [dirname($_SERVER['SCRIPT_FILENAME'])](stackoverflow.com/questions/10525880/…)

– Willker Moraes Silva
Feb 16 '17 at 2:07












1 Answer
1






active

oldest

votes


















1














The best approximation for your case is dirname($_SERVER['SCRIPT_FILENAME']) to get the "instance" dir, if you can live with the general warning in the doc about the $_SERVER vars:




There is no guarantee that every web server will provide any of these;
servers may omit some, or provide others not listed here.




Also, the PHP manual does not explicitly promise that symlinks will be preserved, but that seems to be the case (unless of course your server thinks otherwise). See also this answer.



(Note: this also works in CLI mode. If the script was launched via a relative path (to the CWD), PHP will keep it that way, so you may end up with "." then.)



However, for the general problem (of getting any script's dir with symlinks preserved), this doesn't work. It only works for the script that has been reached via HTTP. Since any other scripts you include later on will all have the same $_SERVER['SCRIPT_FILENAME'] (of course), regardless of their location, dirname($_SERVER['SCRIPT_FILENAME']) will yield the wrong dir if they are located elsewhere...



I don't think PHP has a solution for that today (as of v7.2).



(And you were correct initially: PHP is doing the symlink-resolving for __FILE__, irrespective of your server. And it's unlikely that this would ever change, because it has stayed this way for too long (and too much code depends on it), even though there's realpath() if we wanted the resolved path, while there's no bullet-proof solution for the symlinked case.)






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37446267%2fhave-script-path-with-unresolved-symlinks-in-php%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The best approximation for your case is dirname($_SERVER['SCRIPT_FILENAME']) to get the "instance" dir, if you can live with the general warning in the doc about the $_SERVER vars:




    There is no guarantee that every web server will provide any of these;
    servers may omit some, or provide others not listed here.




    Also, the PHP manual does not explicitly promise that symlinks will be preserved, but that seems to be the case (unless of course your server thinks otherwise). See also this answer.



    (Note: this also works in CLI mode. If the script was launched via a relative path (to the CWD), PHP will keep it that way, so you may end up with "." then.)



    However, for the general problem (of getting any script's dir with symlinks preserved), this doesn't work. It only works for the script that has been reached via HTTP. Since any other scripts you include later on will all have the same $_SERVER['SCRIPT_FILENAME'] (of course), regardless of their location, dirname($_SERVER['SCRIPT_FILENAME']) will yield the wrong dir if they are located elsewhere...



    I don't think PHP has a solution for that today (as of v7.2).



    (And you were correct initially: PHP is doing the symlink-resolving for __FILE__, irrespective of your server. And it's unlikely that this would ever change, because it has stayed this way for too long (and too much code depends on it), even though there's realpath() if we wanted the resolved path, while there's no bullet-proof solution for the symlinked case.)






    share|improve this answer






























      1














      The best approximation for your case is dirname($_SERVER['SCRIPT_FILENAME']) to get the "instance" dir, if you can live with the general warning in the doc about the $_SERVER vars:




      There is no guarantee that every web server will provide any of these;
      servers may omit some, or provide others not listed here.




      Also, the PHP manual does not explicitly promise that symlinks will be preserved, but that seems to be the case (unless of course your server thinks otherwise). See also this answer.



      (Note: this also works in CLI mode. If the script was launched via a relative path (to the CWD), PHP will keep it that way, so you may end up with "." then.)



      However, for the general problem (of getting any script's dir with symlinks preserved), this doesn't work. It only works for the script that has been reached via HTTP. Since any other scripts you include later on will all have the same $_SERVER['SCRIPT_FILENAME'] (of course), regardless of their location, dirname($_SERVER['SCRIPT_FILENAME']) will yield the wrong dir if they are located elsewhere...



      I don't think PHP has a solution for that today (as of v7.2).



      (And you were correct initially: PHP is doing the symlink-resolving for __FILE__, irrespective of your server. And it's unlikely that this would ever change, because it has stayed this way for too long (and too much code depends on it), even though there's realpath() if we wanted the resolved path, while there's no bullet-proof solution for the symlinked case.)






      share|improve this answer




























        1












        1








        1







        The best approximation for your case is dirname($_SERVER['SCRIPT_FILENAME']) to get the "instance" dir, if you can live with the general warning in the doc about the $_SERVER vars:




        There is no guarantee that every web server will provide any of these;
        servers may omit some, or provide others not listed here.




        Also, the PHP manual does not explicitly promise that symlinks will be preserved, but that seems to be the case (unless of course your server thinks otherwise). See also this answer.



        (Note: this also works in CLI mode. If the script was launched via a relative path (to the CWD), PHP will keep it that way, so you may end up with "." then.)



        However, for the general problem (of getting any script's dir with symlinks preserved), this doesn't work. It only works for the script that has been reached via HTTP. Since any other scripts you include later on will all have the same $_SERVER['SCRIPT_FILENAME'] (of course), regardless of their location, dirname($_SERVER['SCRIPT_FILENAME']) will yield the wrong dir if they are located elsewhere...



        I don't think PHP has a solution for that today (as of v7.2).



        (And you were correct initially: PHP is doing the symlink-resolving for __FILE__, irrespective of your server. And it's unlikely that this would ever change, because it has stayed this way for too long (and too much code depends on it), even though there's realpath() if we wanted the resolved path, while there's no bullet-proof solution for the symlinked case.)






        share|improve this answer















        The best approximation for your case is dirname($_SERVER['SCRIPT_FILENAME']) to get the "instance" dir, if you can live with the general warning in the doc about the $_SERVER vars:




        There is no guarantee that every web server will provide any of these;
        servers may omit some, or provide others not listed here.




        Also, the PHP manual does not explicitly promise that symlinks will be preserved, but that seems to be the case (unless of course your server thinks otherwise). See also this answer.



        (Note: this also works in CLI mode. If the script was launched via a relative path (to the CWD), PHP will keep it that way, so you may end up with "." then.)



        However, for the general problem (of getting any script's dir with symlinks preserved), this doesn't work. It only works for the script that has been reached via HTTP. Since any other scripts you include later on will all have the same $_SERVER['SCRIPT_FILENAME'] (of course), regardless of their location, dirname($_SERVER['SCRIPT_FILENAME']) will yield the wrong dir if they are located elsewhere...



        I don't think PHP has a solution for that today (as of v7.2).



        (And you were correct initially: PHP is doing the symlink-resolving for __FILE__, irrespective of your server. And it's unlikely that this would ever change, because it has stayed this way for too long (and too much code depends on it), even though there's realpath() if we wanted the resolved path, while there's no bullet-proof solution for the symlinked case.)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 19:21

























        answered Nov 13 '18 at 1:30









        Sz.Sz.

        1,3111824




        1,3111824






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37446267%2fhave-script-path-with-unresolved-symlinks-in-php%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)