Create temporary URL to download files












-1














I am making a project in which I will send File URL on mail to download the file.



For hosting my files I am using Mega and saved Mega file's URL to the database.



The script will fetch the Mega file's URL from the database, but here I want to shorten the Mega file's URL to make it available for 3 hours only and after that, shorten URL will expire.










share|improve this question





























    -1














    I am making a project in which I will send File URL on mail to download the file.



    For hosting my files I am using Mega and saved Mega file's URL to the database.



    The script will fetch the Mega file's URL from the database, but here I want to shorten the Mega file's URL to make it available for 3 hours only and after that, shorten URL will expire.










    share|improve this question



























      -1












      -1








      -1







      I am making a project in which I will send File URL on mail to download the file.



      For hosting my files I am using Mega and saved Mega file's URL to the database.



      The script will fetch the Mega file's URL from the database, but here I want to shorten the Mega file's URL to make it available for 3 hours only and after that, shorten URL will expire.










      share|improve this question















      I am making a project in which I will send File URL on mail to download the file.



      For hosting my files I am using Mega and saved Mega file's URL to the database.



      The script will fetch the Mega file's URL from the database, but here I want to shorten the Mega file's URL to make it available for 3 hours only and after that, shorten URL will expire.







      php






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 7:22









      Kirk Beard

      6,603103038




      6,603103038










      asked Nov 21 '18 at 7:08









      StrangeStrange

      406




      406
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can simply create any random URL you want on your server, map it to the entry in the database and if someone requests the URL during the time it's valid redirect the request to the actual mega URL you store in the database.



          How you decide to "shorten" the URL is up to you and depends mostly on aesthetics.



          Perhaps one wants




          www.mysit.com/file.php?uid=8s9f1e3f2v78d9f172fd3easvcd9871




          or




          www.mysite.com/ge723gdn328




          etc.



          When you store a file store the time it should be valid until the actual URL it points to and the unique id of the file which will be part of the "shortened" URL you provide. For instance, it could be a hash (or some friendly identifier) of the URL of the actual file and then you will be quite certain it's unique.



          When a request is made, grab the unique id, fetch the data from the database, redirect (or proxy) the contents of the actual file.






          share|improve this answer





























            1














            You can try Bitly for shorten the URL. You need to register with them and get access token.



            Using below URL you can generate the short URL. You just need to pass long URL as input with an access token.



            https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=http%3A%2F%2Fgoogle.com%2F



            OR



            /* returns the shortened url */
            function get_bitly_short_url($url,$login,$appkey,$format='txt') {
            $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
            return curl_get_result($connectURL);
            }

            /* returns expanded url */
            function get_bitly_long_url($url,$login,$appkey,$format='txt') {
            $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
            return curl_get_result($connectURL);
            }

            /* returns a result form url */
            function curl_get_result($url) {
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $data = curl_exec($ch);
            curl_close($ch);
            return $data;
            }

            /* get the short url */
            $short_url = get_bitly_short_url('https://google.com/','loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

            /* get the long url from the short one */
            $long_url = get_bitly_long_url($short_url,'loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');





            share|improve this answer





























              0














              My idea for solution would be for you to add new column in database where you store Mega file's URL, something like 'shortURL', where you will generate some string and save it there. Then, when user accesses 'yourdomain.com/randomString', he/she will be redirected to the Mega file's URL. Last thing you'd need to do is a cron job that will run on server, and would delete the shorURL after some time (3 hours). If user accesses the 'yourdomain.com/randomString' after the time is up, he will be redirected to homepage or error page will appear saying that the link has expired.



              I guess there are some other ways to do what you want, but this is the first one that comes to my mind.






              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%2f53406897%2fcreate-temporary-url-to-download-files%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You can simply create any random URL you want on your server, map it to the entry in the database and if someone requests the URL during the time it's valid redirect the request to the actual mega URL you store in the database.



                How you decide to "shorten" the URL is up to you and depends mostly on aesthetics.



                Perhaps one wants




                www.mysit.com/file.php?uid=8s9f1e3f2v78d9f172fd3easvcd9871




                or




                www.mysite.com/ge723gdn328




                etc.



                When you store a file store the time it should be valid until the actual URL it points to and the unique id of the file which will be part of the "shortened" URL you provide. For instance, it could be a hash (or some friendly identifier) of the URL of the actual file and then you will be quite certain it's unique.



                When a request is made, grab the unique id, fetch the data from the database, redirect (or proxy) the contents of the actual file.






                share|improve this answer


























                  1














                  You can simply create any random URL you want on your server, map it to the entry in the database and if someone requests the URL during the time it's valid redirect the request to the actual mega URL you store in the database.



                  How you decide to "shorten" the URL is up to you and depends mostly on aesthetics.



                  Perhaps one wants




                  www.mysit.com/file.php?uid=8s9f1e3f2v78d9f172fd3easvcd9871




                  or




                  www.mysite.com/ge723gdn328




                  etc.



                  When you store a file store the time it should be valid until the actual URL it points to and the unique id of the file which will be part of the "shortened" URL you provide. For instance, it could be a hash (or some friendly identifier) of the URL of the actual file and then you will be quite certain it's unique.



                  When a request is made, grab the unique id, fetch the data from the database, redirect (or proxy) the contents of the actual file.






                  share|improve this answer
























                    1












                    1








                    1






                    You can simply create any random URL you want on your server, map it to the entry in the database and if someone requests the URL during the time it's valid redirect the request to the actual mega URL you store in the database.



                    How you decide to "shorten" the URL is up to you and depends mostly on aesthetics.



                    Perhaps one wants




                    www.mysit.com/file.php?uid=8s9f1e3f2v78d9f172fd3easvcd9871




                    or




                    www.mysite.com/ge723gdn328




                    etc.



                    When you store a file store the time it should be valid until the actual URL it points to and the unique id of the file which will be part of the "shortened" URL you provide. For instance, it could be a hash (or some friendly identifier) of the URL of the actual file and then you will be quite certain it's unique.



                    When a request is made, grab the unique id, fetch the data from the database, redirect (or proxy) the contents of the actual file.






                    share|improve this answer












                    You can simply create any random URL you want on your server, map it to the entry in the database and if someone requests the URL during the time it's valid redirect the request to the actual mega URL you store in the database.



                    How you decide to "shorten" the URL is up to you and depends mostly on aesthetics.



                    Perhaps one wants




                    www.mysit.com/file.php?uid=8s9f1e3f2v78d9f172fd3easvcd9871




                    or




                    www.mysite.com/ge723gdn328




                    etc.



                    When you store a file store the time it should be valid until the actual URL it points to and the unique id of the file which will be part of the "shortened" URL you provide. For instance, it could be a hash (or some friendly identifier) of the URL of the actual file and then you will be quite certain it's unique.



                    When a request is made, grab the unique id, fetch the data from the database, redirect (or proxy) the contents of the actual file.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 8:00









                    inquaminquam

                    7,378134691




                    7,378134691

























                        1














                        You can try Bitly for shorten the URL. You need to register with them and get access token.



                        Using below URL you can generate the short URL. You just need to pass long URL as input with an access token.



                        https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=http%3A%2F%2Fgoogle.com%2F



                        OR



                        /* returns the shortened url */
                        function get_bitly_short_url($url,$login,$appkey,$format='txt') {
                        $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
                        return curl_get_result($connectURL);
                        }

                        /* returns expanded url */
                        function get_bitly_long_url($url,$login,$appkey,$format='txt') {
                        $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
                        return curl_get_result($connectURL);
                        }

                        /* returns a result form url */
                        function curl_get_result($url) {
                        $ch = curl_init();
                        $timeout = 5;
                        curl_setopt($ch,CURLOPT_URL,$url);
                        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
                        $data = curl_exec($ch);
                        curl_close($ch);
                        return $data;
                        }

                        /* get the short url */
                        $short_url = get_bitly_short_url('https://google.com/','loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

                        /* get the long url from the short one */
                        $long_url = get_bitly_long_url($short_url,'loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');





                        share|improve this answer


























                          1














                          You can try Bitly for shorten the URL. You need to register with them and get access token.



                          Using below URL you can generate the short URL. You just need to pass long URL as input with an access token.



                          https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=http%3A%2F%2Fgoogle.com%2F



                          OR



                          /* returns the shortened url */
                          function get_bitly_short_url($url,$login,$appkey,$format='txt') {
                          $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
                          return curl_get_result($connectURL);
                          }

                          /* returns expanded url */
                          function get_bitly_long_url($url,$login,$appkey,$format='txt') {
                          $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
                          return curl_get_result($connectURL);
                          }

                          /* returns a result form url */
                          function curl_get_result($url) {
                          $ch = curl_init();
                          $timeout = 5;
                          curl_setopt($ch,CURLOPT_URL,$url);
                          curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                          curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
                          $data = curl_exec($ch);
                          curl_close($ch);
                          return $data;
                          }

                          /* get the short url */
                          $short_url = get_bitly_short_url('https://google.com/','loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

                          /* get the long url from the short one */
                          $long_url = get_bitly_long_url($short_url,'loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');





                          share|improve this answer
























                            1












                            1








                            1






                            You can try Bitly for shorten the URL. You need to register with them and get access token.



                            Using below URL you can generate the short URL. You just need to pass long URL as input with an access token.



                            https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=http%3A%2F%2Fgoogle.com%2F



                            OR



                            /* returns the shortened url */
                            function get_bitly_short_url($url,$login,$appkey,$format='txt') {
                            $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
                            return curl_get_result($connectURL);
                            }

                            /* returns expanded url */
                            function get_bitly_long_url($url,$login,$appkey,$format='txt') {
                            $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
                            return curl_get_result($connectURL);
                            }

                            /* returns a result form url */
                            function curl_get_result($url) {
                            $ch = curl_init();
                            $timeout = 5;
                            curl_setopt($ch,CURLOPT_URL,$url);
                            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
                            $data = curl_exec($ch);
                            curl_close($ch);
                            return $data;
                            }

                            /* get the short url */
                            $short_url = get_bitly_short_url('https://google.com/','loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

                            /* get the long url from the short one */
                            $long_url = get_bitly_long_url($short_url,'loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');





                            share|improve this answer












                            You can try Bitly for shorten the URL. You need to register with them and get access token.



                            Using below URL you can generate the short URL. You just need to pass long URL as input with an access token.



                            https://api-ssl.bitly.com/v3/shorten?access_token=ACCESS_TOKEN&longUrl=http%3A%2F%2Fgoogle.com%2F



                            OR



                            /* returns the shortened url */
                            function get_bitly_short_url($url,$login,$appkey,$format='txt') {
                            $connectURL = 'http://api.bit.ly/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
                            return curl_get_result($connectURL);
                            }

                            /* returns expanded url */
                            function get_bitly_long_url($url,$login,$appkey,$format='txt') {
                            $connectURL = 'http://api.bit.ly/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
                            return curl_get_result($connectURL);
                            }

                            /* returns a result form url */
                            function curl_get_result($url) {
                            $ch = curl_init();
                            $timeout = 5;
                            curl_setopt($ch,CURLOPT_URL,$url);
                            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
                            $data = curl_exec($ch);
                            curl_close($ch);
                            return $data;
                            }

                            /* get the short url */
                            $short_url = get_bitly_short_url('https://google.com/','loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

                            /* get the long url from the short one */
                            $long_url = get_bitly_long_url($short_url,'loginname','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 7:31









                            Pankaj DadurePankaj Dadure

                            561310




                            561310























                                0














                                My idea for solution would be for you to add new column in database where you store Mega file's URL, something like 'shortURL', where you will generate some string and save it there. Then, when user accesses 'yourdomain.com/randomString', he/she will be redirected to the Mega file's URL. Last thing you'd need to do is a cron job that will run on server, and would delete the shorURL after some time (3 hours). If user accesses the 'yourdomain.com/randomString' after the time is up, he will be redirected to homepage or error page will appear saying that the link has expired.



                                I guess there are some other ways to do what you want, but this is the first one that comes to my mind.






                                share|improve this answer


























                                  0














                                  My idea for solution would be for you to add new column in database where you store Mega file's URL, something like 'shortURL', where you will generate some string and save it there. Then, when user accesses 'yourdomain.com/randomString', he/she will be redirected to the Mega file's URL. Last thing you'd need to do is a cron job that will run on server, and would delete the shorURL after some time (3 hours). If user accesses the 'yourdomain.com/randomString' after the time is up, he will be redirected to homepage or error page will appear saying that the link has expired.



                                  I guess there are some other ways to do what you want, but this is the first one that comes to my mind.






                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    My idea for solution would be for you to add new column in database where you store Mega file's URL, something like 'shortURL', where you will generate some string and save it there. Then, when user accesses 'yourdomain.com/randomString', he/she will be redirected to the Mega file's URL. Last thing you'd need to do is a cron job that will run on server, and would delete the shorURL after some time (3 hours). If user accesses the 'yourdomain.com/randomString' after the time is up, he will be redirected to homepage or error page will appear saying that the link has expired.



                                    I guess there are some other ways to do what you want, but this is the first one that comes to my mind.






                                    share|improve this answer












                                    My idea for solution would be for you to add new column in database where you store Mega file's URL, something like 'shortURL', where you will generate some string and save it there. Then, when user accesses 'yourdomain.com/randomString', he/she will be redirected to the Mega file's URL. Last thing you'd need to do is a cron job that will run on server, and would delete the shorURL after some time (3 hours). If user accesses the 'yourdomain.com/randomString' after the time is up, he will be redirected to homepage or error page will appear saying that the link has expired.



                                    I guess there are some other ways to do what you want, but this is the first one that comes to my mind.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 21 '18 at 7:15









                                    Mirza MašićMirza Mašić

                                    6915




                                    6915






























                                        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.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • 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%2f53406897%2fcreate-temporary-url-to-download-files%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

                                        Create new schema in PostgreSQL using DBeaver

                                        Deepest pit of an array with Javascript: test on Codility

                                        Costa Masnaga