Creating new variable from another via string manipulation












0















Suppose, I have a variable $var1 which contains the followings:



$var1="../sidebar_items/uploaded_files/notices/circular.pdf";


Now, I want to create another variable $var2 which will hold the following value:



$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";


I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?










share|improve this question

























  • I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

    – Ultimater
    Nov 22 '18 at 6:54
















0















Suppose, I have a variable $var1 which contains the followings:



$var1="../sidebar_items/uploaded_files/notices/circular.pdf";


Now, I want to create another variable $var2 which will hold the following value:



$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";


I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?










share|improve this question

























  • I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

    – Ultimater
    Nov 22 '18 at 6:54














0












0








0








Suppose, I have a variable $var1 which contains the followings:



$var1="../sidebar_items/uploaded_files/notices/circular.pdf";


Now, I want to create another variable $var2 which will hold the following value:



$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";


I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?










share|improve this question
















Suppose, I have a variable $var1 which contains the followings:



$var1="../sidebar_items/uploaded_files/notices/circular.pdf";


Now, I want to create another variable $var2 which will hold the following value:



$var2="../dev/sidebar_items/uploaded_files/notices/circular.pdf";


I want to create $var2 with the help of $var1 via string manipulation. Is it possible to do so? How will I achieve that?







php string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:37









Magnus Eriksson

7,42641328




7,42641328










asked Nov 22 '18 at 6:26









user10689421user10689421

1




1













  • I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

    – Ultimater
    Nov 22 '18 at 6:54



















  • I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

    – Ultimater
    Nov 22 '18 at 6:54

















I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

– Ultimater
Nov 22 '18 at 6:54





I'd refactor the whole system so you'd have just: $filename="/sidebar_items/uploaded_files/notices/circular.pdf"; The responsibility of deciding what directory to look in (.. or ../dev) is better kept to a dedicated part of the application for determining this. This way you keep the application responsibilities more loosely coupled and thus more maintainable.

– Ultimater
Nov 22 '18 at 6:54












4 Answers
4






active

oldest

votes


















1














Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:



$var2 = str_replace('../', '../dev/', $var1);





share|improve this answer
























  • I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

    – kungphu
    Nov 22 '18 at 6:55



















0














It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php would be an obvious target.



You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.






share|improve this answer































    0














    you could also use strtr() function of PHP



    $var2 = strtr($var1, '../', '../dev/');





    share|improve this answer































      0














      I'd approach it by separating out the file name using basename() and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...



      $var1="../sidebar_items/uploaded_files/notices/circular.pdf";
      $devpath = "../dev/sidebar_items/uploaded_files/notices/";
      echo $devpath.basename($var1);


      gives...



      ../dev/sidebar_items/uploaded_files/notices/circular.pdf





      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%2f53425016%2fcreating-new-variable-from-another-via-string-manipulation%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:



        $var2 = str_replace('../', '../dev/', $var1);





        share|improve this answer
























        • I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

          – kungphu
          Nov 22 '18 at 6:55
















        1














        Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:



        $var2 = str_replace('../', '../dev/', $var1);





        share|improve this answer
























        • I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

          – kungphu
          Nov 22 '18 at 6:55














        1












        1








        1







        Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:



        $var2 = str_replace('../', '../dev/', $var1);





        share|improve this answer













        Just use str_replace(). It returns the changed string but doesn't change the original variable so you can store the result in a new variable:



        $var2 = str_replace('../', '../dev/', $var1);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 6:32









        Magnus ErikssonMagnus Eriksson

        7,42641328




        7,42641328













        • I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

          – kungphu
          Nov 22 '18 at 6:55



















        • I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

          – kungphu
          Nov 22 '18 at 6:55

















        I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

        – kungphu
        Nov 22 '18 at 6:55





        I think this answer would be improved by limiting the replacement to the beginning of the string, rather than all instances of ../ throughout the string, since we don't know how user-uploaded filenames are being handled.

        – kungphu
        Nov 22 '18 at 6:55













        0














        It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php would be an obvious target.



        You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.






        share|improve this answer




























          0














          It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php would be an obvious target.



          You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.






          share|improve this answer


























            0












            0








            0







            It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php would be an obvious target.



            You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.






            share|improve this answer













            It looks like you're allowing users to upload files and saving the filenames. That's potentially very dangerous depending on how you're dealing with them; if you are taking their string value and doing file system operations against it, you could end up with a user uploading a file with a name like ../../../../../usr/bin/php and risking allowing a delete operation against that file (if your permissions are set up really, really poorly) or, perhaps more realistically, using path manipulation to delete, modify, or overwrite any file owned by the web server user. index.php would be an obvious target.



            You should consider keeping both paths in separate constants rather than using string manipulation to turn one into the other at runtime. You should also consider renaming user-uploaded files, or at least being very careful about how you store them with regard to naming based on how you access them in your code.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 6:52









            kungphukungphu

            2,81911425




            2,81911425























                0














                you could also use strtr() function of PHP



                $var2 = strtr($var1, '../', '../dev/');





                share|improve this answer




























                  0














                  you could also use strtr() function of PHP



                  $var2 = strtr($var1, '../', '../dev/');





                  share|improve this answer


























                    0












                    0








                    0







                    you could also use strtr() function of PHP



                    $var2 = strtr($var1, '../', '../dev/');





                    share|improve this answer













                    you could also use strtr() function of PHP



                    $var2 = strtr($var1, '../', '../dev/');






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 7:14









                    Parvej AlamParvej Alam

                    2168




                    2168























                        0














                        I'd approach it by separating out the file name using basename() and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...



                        $var1="../sidebar_items/uploaded_files/notices/circular.pdf";
                        $devpath = "../dev/sidebar_items/uploaded_files/notices/";
                        echo $devpath.basename($var1);


                        gives...



                        ../dev/sidebar_items/uploaded_files/notices/circular.pdf





                        share|improve this answer




























                          0














                          I'd approach it by separating out the file name using basename() and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...



                          $var1="../sidebar_items/uploaded_files/notices/circular.pdf";
                          $devpath = "../dev/sidebar_items/uploaded_files/notices/";
                          echo $devpath.basename($var1);


                          gives...



                          ../dev/sidebar_items/uploaded_files/notices/circular.pdf





                          share|improve this answer


























                            0












                            0








                            0







                            I'd approach it by separating out the file name using basename() and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...



                            $var1="../sidebar_items/uploaded_files/notices/circular.pdf";
                            $devpath = "../dev/sidebar_items/uploaded_files/notices/";
                            echo $devpath.basename($var1);


                            gives...



                            ../dev/sidebar_items/uploaded_files/notices/circular.pdf





                            share|improve this answer













                            I'd approach it by separating out the file name using basename() and then having a variable which has the path to the dev directory. This allows you to change it to all sorts rather than limiting it to a minor change...



                            $var1="../sidebar_items/uploaded_files/notices/circular.pdf";
                            $devpath = "../dev/sidebar_items/uploaded_files/notices/";
                            echo $devpath.basename($var1);


                            gives...



                            ../dev/sidebar_items/uploaded_files/notices/circular.pdf






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 7:21









                            Nigel RenNigel Ren

                            28.2k62034




                            28.2k62034






























                                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%2f53425016%2fcreating-new-variable-from-another-via-string-manipulation%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







                                這個網誌中的熱門文章

                                Tangent Lines Diagram Along Smooth Curve

                                Yusuf al-Mu'taman ibn Hud

                                Zucchini