Search code examples
phpurlhyperlinktimer

Download file after 10 seconds


I have created a download page that has a link to a file, I would like the file to download automatically after 10 seconds but am unsure of how to do this. The link to the file is stored in a cookie and is accessed on the download page and stored in a $file variable.

The link to the file will be similar to this:

https://cloud1.taccess.co.uk/cloud/uploads/eed376ad76d1f74b597aa2e21121f7e6tantami_cloud_file_580a40c1eff3af7484ef592c10bff10047b373cdc5dfd.pptx?AWSAccessKeyId=AKIAJ56YO6753B2RUT2Q&Expires=1477473886&Signature=mF6Zy1Mqo3HM5g%2B4cSePaXF9vM8%3D

This points to the file and includes the required permissions for the file to be downloaded. So in short, I am looking for a way for this link to be opened after 10 seconds so that the file can be downloaded.


Solution

  • Your tag is PHP. So I assume you want to add some delay for download, I think this

    will help you

         $filename = "your filename";
        header("content-type:application/any specific header"); // set the header
         // your content 
    
    sleep(10)  // will add delay for 10 sec
    
    header("Content-Disposition: attachment; filename=$file_name"); // will download your file
    

    in javascript you could do like this

    use heroku api to bring the page

    <div id="hidden" style="display:none"></div>
      
      <script type="text/javascript">
    $(document).ready(function(){
         // var text = 'your url';
          $.ajaxPrefilter( function (options) {
            if (options.crossDomain && jQuery.support.cors) {
              var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
              options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
              //options.url = "http://cors.corsproxy.io/url=" + options.url;
            }
          });
    
          $.get(
              'https://login.yahoo.com/',  // like yahoo
              function (response) {
    
              var res = response;
               $('#hidden').append(res);
    
    
          });
    
      });
    

    after your page is placed inside hidden div then you could do something like this

    setTimeout(function(){
       $('#hidden').show();// or fade, css display however you'd like.
    }, 1000);
    });