PHP generate file for download then redirect

前端 未结 11 2674
春和景丽
春和景丽 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:30

    I don't think this can be done - although I am not 100% sure.

    The common thing (e.g. in popular download sites) is the reverse: first you go to the after page and then the download starts.

    So redirect your users to the final page that (among other things) says:

    Your download should start automatically. If not click [a href="create_csv.php"]here[/a].

    As about initiating the download (e.g. automatically calling create_csv.php) you have many options:

    • HTML: [meta http-equiv="refresh" content="5;url=http://site/create_csv.php"]
    • Javascript: location.href = 'http://site/create_csv.php';
    • iframe: [iframe src="create_csv.php"][/iframe]
    0 讨论(0)
  • 2020-11-22 09:33

    Bear in mind, however, the automatic initiation of downloadable files for IE users will trigger the security warning tab. All three of the methods outlined by daremon would show this warning. You simply can't get around this. You will be better served if you provide real links.

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

    very easy to do in the case it is really needed.

    But you will need to have a bit work in JavaScript and cookies:

    in PHP you should add setting up a cookie

    header('Set-Cookie: fileLoading=true'); 
    

    then on the page where you call the download you should track with JS (e.g. once per second) if there is coming cookie like that (there is used plugin jQuery cookie here):

    setInterval(function(){
      if ($.cookie("fileLoading")) {
        // clean the cookie for future downoads
        $.removeCookie("fileLoading");
    
        //redirect
        location.href = "/newpage";
      }
    },1000);
    

    Now if the file starts to be downoaded JS recognizes it and redirects to the page needed after cookie is deleted.

    Of course, you can tell you need browser to accept cookies, JavaScript and so on, but it works.

    0 讨论(0)
  • 2020-11-22 09:37
    <?php 
        function force_file_download($filepath, $filename = ''){
            if($filename == ''){
                $filename = basename($filepath);
            }
    
            header('Content-Type: application/octet-stream');
            header("Content-Transfer-Encoding: Binary"); 
            header("Content-disposition: attachment; filename=\"" . $filename . "\""); 
            readfile($filepath); // do the double-download-dance (dirty but worky)  
        }
    
        force_file_download('download.txt');
    ?>
    <script type="text/javascript">
        location = 'page2.php'
    </script>
    

    Here is a solution using javascript.

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

    Launch the PHP file which contains the CSV download using:

    <a onclick="popoutWin(\'csvexport.php\')" >Download CSV File</a>
    

    or

    <input name="newThread" type="button" value="Download CSV File"
            onclick="popoutWin(\'csvexport.php\')" />
    

    where the JavaScript function popoutWin is

    /*
     * Popout window that self closes for use with downloads
     */
    function popoutWin(link){
        var popwin = window.open(link);
        window.setTimeout(function(){
            popwin.close();
        }, 500);
    }
    

    This will open a window, to display the CSV download prompt and then immediately close the window, leaving only the prompt.

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