PHP generate file for download then redirect

前端 未结 11 2673
春和景丽
春和景丽 2020-11-22 09:08

I have a PHP app that creates a CSV file which is forced to download using headers. Here\'s the relevant part of the code:

header(\'Content-Type: applicatio         


        
相关标签:
11条回答
  • 2020-11-22 09:12

    The header you are sending are HTTP headers. The browser takes that as a page request and processes it as a page. And in your case, a page it needs to download.

    So adding a redirect header to that confuses the whole process of downloading the file (since headers are collected, generated into one header and then sent to the browser, you can try this by setting multiple redirect headers IIRC)

    0 讨论(0)
  • 2020-11-22 09:12

    You can try and redirect to the URL plus a parameter that represents the file contents. And in the redirect, you can output the file content for download.

    0 讨论(0)
  • 2020-11-22 09:14

    Here is the answer:
    It's work!
    You need three different parts of code:

    HTML

    <a id="download_btn" class="btn btn-primary" href="?file=filename&download=1">Download<&/a>
    


    JQuery

    $('#download_btn').click(function(){
        window.location.href = '<?=base_url()?>/?file=<?=$file;?>&download=1';
    }).focusout (function(){
        window.location.href = '<?=base_url()?>';
        return false;
    });
    


    PHP

            if(isset($_GET['download']) && isset($_GET['file'])){
    
                $zip_path = 'path_to/'.$_GET['file'].'.zip';
    
                if(file_exists($zip_path)){
                    header('Content-Type: application/zip');
                    header('Content-Disposition: attachment; filename="'.basename($zip_path).'"');
                    header('Content-Length: ' . filesize($zip_path));
                    header('Location: '.$zip_path);
                }
    
            }
    
    0 讨论(0)
  • 2020-11-22 09:15

    I found one workaround for this that relies on javascript, so it's not exactly secure, but for non-secure critical sites it seems to work.

    Have a form with a button titled 'download' with the action set to point to the download script, then using javascript put something on the onsubmit handler that strips out the download button and replaces the messaging on the screen. The download should still happen and the screen will change. Obviously, if there's an issue with the download script then it still looks like the download was successful even if it doesn't fire, but it's the best I've got right now.

    0 讨论(0)
  • 2020-11-22 09:20

    This is quite old issue, but here is how I achieved it via JS.

    // Capture the "click" event of the link.
    var link = document.getElementById("the-link");
    link.addEventListener("click", function(evt) {
      // Stop the link from doing what it would normally do.
      evt.preventDefault();
      // Open the file download in a new window. (It should just
      // show a normal file dialog)
      window.open(this.href, "_blank");
      // Then redirect the page you are on to whatever page you
      // want shown once the download has been triggered.
      window.location = "/thank_you.html";
    }, true);
    

    Via - https://www.daniweb.com/web-development/php/threads/463652/page-not-redirecting-after-sending-headers-in-php

    0 讨论(0)
  • 2020-11-22 09:22

    Hmmmm...

    Put the code below in the main javascript of the site.

     if (window.location.pathname == '/url_of_redirect/') {
            window.open(location.protocol + '//' + location.hostname + '/url_of_download.zip', '_parent');
        }
    

    Explaining: It does the normal redirect with php to some url you will difine and in javascipt it says: If the pathname is the same as the path that redirect made '/url_of_redirect/' then it opens the url on a new page, generating the downlod.

    0 讨论(0)
提交回复
热议问题