开发者

Is it possible to redirect a page after a readfile()?

Is it possible to redirect a page after a readfile()?

I tried to use Header, but that doesn't work. Also the meta redirect doesn't work either:

header ("Content-Type: application/pdf");

header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $开发者_如何学编程size");

readfile($file_path);  // Giving file to the customer
// header('Location:../checkout.php');  Doesn't work
// echo ('<meta http-equiv="refresh" content="1;url=../checkout.php">');  Doesn't work


It's impossible to output the header after you have output the file. Headers need to go first (hence the name). If you would output the header before you output the file, a browser would be free to ignore the body. I'm not sure what browsers actually do, but I doubt both a redirect and a file download will be initiated simultaneously. One or the other would probably be ignored.

Basic fact: You cannot output any information apart from what's necessary for the file download in the same response as the file download. The header of the file download response can only contain information regarding the file, the response body can only contain the file (i.e. no <meta> refreshes or such, those would become part of the file).

To illustrate, this would be what the browser receives:

HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Disposition: attachment; filename=foo
Content-Length: 42

woieufhm23umh29… (file contents as binary gibberish) …cQ3985fno1rHtn3f
qo8923r10d30<meta http-equiv="refresh" content="1;url=../checkout.php">

Everything after the first blank line would be saved as the file contents, including the <meta> tag. You would actually corrupt the file by outputting any additional content there.

The way this usually works is to present the user with a "download page", which has an auto-refresh meta tag and/or Javascript, which first initiates the download, then on the second refresh redirects.


I had a workaround. The page where I send the file I opened with a target="_blank", on first page I did a onclick JavaScript redirect or show div.

The page that opens to send the file, starts the download and immediately closes.

Hope this helps.


Your code immediately sends the file contents to the user, i.e. it starts a download. If you want to start the download and display another page, display the page and - on this page - send this header: header('Refresh: 2;url=url/to/your/download');

As the browser will start a download it will not redirect away from the displayed page.


Just posting this as I was working on this for a minute too trying to redirect and download a file after a user submits a form. Solution was embedding the PHP file that runs readfileDownloader.php in an iframe on the form processing page, then refreshing on that page works. My readfileDownloader.php uses application/octet-stream to avoid the iFrame potentially trying to actually display the target file.

    // iFrame to load the file downloader php file
    echo '<iframe id="my_iframe" style="display:none;"></iframe>';
    echo '<script>';
    echo "document.getElementById('my_iframe').src = \"readfileDownloader.php\";";
    echo '</script>';

    // Redirects. Refresh must at least be 1.
    header('Refresh: 1;url=final_location.html');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜